msm_drv.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. /*
  2. * Copyright (c) 2016-2020, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * Copyright (c) 2016 Intel Corporation
  20. *
  21. * Permission to use, copy, modify, distribute, and sell this software and its
  22. * documentation for any purpose is hereby granted without fee, provided that
  23. * the above copyright notice appear in all copies and that both that copyright
  24. * notice and this permission notice appear in supporting documentation, and
  25. * that the name of the copyright holders not be used in advertising or
  26. * publicity pertaining to distribution of the software without specific,
  27. * written prior permission. The copyright holders make no representations
  28. * about the suitability of this software for any purpose. It is provided "as
  29. * is" without express or implied warranty.
  30. *
  31. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  32. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  33. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  34. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  35. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  36. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  37. * OF THIS SOFTWARE.
  38. */
  39. #include <linux/of_address.h>
  40. #include <linux/kthread.h>
  41. #include <uapi/linux/sched/types.h>
  42. #include <drm/drm_of.h>
  43. #include <drm/drm_probe_helper.h>
  44. #include "msm_drv.h"
  45. #include "msm_gem.h"
  46. #include "msm_kms.h"
  47. #include "msm_mmu.h"
  48. #include "sde_wb.h"
  49. #include "sde_dbg.h"
  50. /*
  51. * MSM driver version:
  52. * - 1.0.0 - initial interface
  53. * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
  54. * - 1.2.0 - adds explicit fence support for submit ioctl
  55. * - 1.3.0 - adds GMEM_BASE + NR_RINGS params, SUBMITQUEUE_NEW +
  56. * SUBMITQUEUE_CLOSE ioctls, and MSM_INFO_IOVA flag for
  57. * MSM_GEM_INFO ioctl.
  58. * - 1.4.0 - softpin, MSM_RELOC_BO_DUMP, and GEM_INFO support to set/get
  59. * GEM object's debug name
  60. */
  61. #define MSM_VERSION_MAJOR 1
  62. #define MSM_VERSION_MINOR 4
  63. #define MSM_VERSION_PATCHLEVEL 0
  64. #define LASTCLOSE_TIMEOUT_MS 500
  65. #define msm_wait_event_timeout(waitq, cond, timeout_ms, ret) \
  66. do { \
  67. ktime_t cur_ktime; \
  68. ktime_t exp_ktime; \
  69. s64 wait_time_jiffies = msecs_to_jiffies(timeout_ms); \
  70. \
  71. exp_ktime = ktime_add_ms(ktime_get(), timeout_ms); \
  72. do { \
  73. ret = wait_event_timeout(waitq, cond, \
  74. wait_time_jiffies); \
  75. cur_ktime = ktime_get(); \
  76. } while ((!cond) && (ret == 0) && \
  77. (ktime_compare_safe(exp_ktime, cur_ktime) > 0));\
  78. } while (0)
  79. static void msm_fb_output_poll_changed(struct drm_device *dev)
  80. {
  81. struct msm_drm_private *priv = NULL;
  82. if (!dev) {
  83. DRM_ERROR("output_poll_changed failed, invalid input\n");
  84. return;
  85. }
  86. priv = dev->dev_private;
  87. if (priv->fbdev)
  88. drm_fb_helper_hotplug_event(priv->fbdev);
  89. }
  90. /**
  91. * msm_atomic_helper_check - validate state object
  92. * @dev: DRM device
  93. * @state: the driver state object
  94. *
  95. * This is a wrapper for the drm_atomic_helper_check to check the modeset
  96. * and state checking for planes. Additionally it checks if any secure
  97. * transition(moving CRTC and planes between secure and non-secure states and
  98. * vice versa) is allowed or not. When going to secure state, planes
  99. * with fb_mode as dir translated only can be staged on the CRTC, and only one
  100. * CRTC should be active.
  101. * Also mixing of secure and non-secure is not allowed.
  102. *
  103. * RETURNS
  104. * Zero for success or -errorno.
  105. */
  106. int msm_atomic_check(struct drm_device *dev,
  107. struct drm_atomic_state *state)
  108. {
  109. struct msm_drm_private *priv;
  110. priv = dev->dev_private;
  111. if (priv && priv->kms && priv->kms->funcs &&
  112. priv->kms->funcs->atomic_check)
  113. return priv->kms->funcs->atomic_check(priv->kms, state);
  114. return drm_atomic_helper_check(dev, state);
  115. }
  116. static const struct drm_mode_config_funcs mode_config_funcs = {
  117. .fb_create = msm_framebuffer_create,
  118. .output_poll_changed = msm_fb_output_poll_changed,
  119. .atomic_check = msm_atomic_check,
  120. .atomic_commit = msm_atomic_commit,
  121. .atomic_state_alloc = msm_atomic_state_alloc,
  122. .atomic_state_clear = msm_atomic_state_clear,
  123. .atomic_state_free = msm_atomic_state_free,
  124. };
  125. static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
  126. .atomic_commit_tail = msm_atomic_commit_tail,
  127. };
  128. #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
  129. static bool reglog = false;
  130. MODULE_PARM_DESC(reglog, "Enable register read/write logging");
  131. module_param(reglog, bool, 0600);
  132. #else
  133. #define reglog 0
  134. #endif
  135. #ifdef CONFIG_DRM_FBDEV_EMULATION
  136. static bool fbdev = true;
  137. MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
  138. module_param(fbdev, bool, 0600);
  139. #endif
  140. static char *vram = "16m";
  141. MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
  142. module_param(vram, charp, 0);
  143. bool dumpstate = false;
  144. MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
  145. module_param(dumpstate, bool, 0600);
  146. static bool modeset = true;
  147. MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)");
  148. module_param(modeset, bool, 0600);
  149. /*
  150. * Util/helpers:
  151. */
  152. int msm_clk_bulk_get(struct device *dev, struct clk_bulk_data **bulk)
  153. {
  154. struct property *prop;
  155. const char *name;
  156. struct clk_bulk_data *local;
  157. int i = 0, ret, count;
  158. count = of_property_count_strings(dev->of_node, "clock-names");
  159. if (count < 1)
  160. return 0;
  161. local = devm_kcalloc(dev, sizeof(struct clk_bulk_data *),
  162. count, GFP_KERNEL);
  163. if (!local)
  164. return -ENOMEM;
  165. of_property_for_each_string(dev->of_node, "clock-names", prop, name) {
  166. local[i].id = devm_kstrdup(dev, name, GFP_KERNEL);
  167. if (!local[i].id) {
  168. devm_kfree(dev, local);
  169. return -ENOMEM;
  170. }
  171. i++;
  172. }
  173. ret = devm_clk_bulk_get(dev, count, local);
  174. if (ret) {
  175. for (i = 0; i < count; i++)
  176. devm_kfree(dev, (void *) local[i].id);
  177. devm_kfree(dev, local);
  178. return ret;
  179. }
  180. *bulk = local;
  181. return count;
  182. }
  183. struct clk *msm_clk_bulk_get_clock(struct clk_bulk_data *bulk, int count,
  184. const char *name)
  185. {
  186. int i;
  187. char n[32];
  188. snprintf(n, sizeof(n), "%s_clk", name);
  189. for (i = 0; bulk && i < count; i++) {
  190. if (!strcmp(bulk[i].id, name) || !strcmp(bulk[i].id, n))
  191. return bulk[i].clk;
  192. }
  193. return NULL;
  194. }
  195. struct clk *msm_clk_get(struct platform_device *pdev, const char *name)
  196. {
  197. struct clk *clk;
  198. char name2[32];
  199. clk = devm_clk_get(&pdev->dev, name);
  200. if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
  201. return clk;
  202. snprintf(name2, sizeof(name2), "%s_clk", name);
  203. clk = devm_clk_get(&pdev->dev, name2);
  204. if (!IS_ERR(clk))
  205. dev_warn(&pdev->dev, "Using legacy clk name binding. Use "
  206. "\"%s\" instead of \"%s\"\n", name, name2);
  207. return clk;
  208. }
  209. void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
  210. const char *dbgname)
  211. {
  212. struct resource *res;
  213. unsigned long size;
  214. void __iomem *ptr;
  215. if (name)
  216. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  217. else
  218. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  219. if (!res) {
  220. dev_dbg(&pdev->dev, "failed to get memory resource: %s\n",
  221. name);
  222. return ERR_PTR(-EINVAL);
  223. }
  224. size = resource_size(res);
  225. ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
  226. if (!ptr) {
  227. dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
  228. return ERR_PTR(-ENOMEM);
  229. }
  230. if (reglog)
  231. dev_dbg(&pdev->dev, "IO:region %s %pK %08lx\n",
  232. dbgname, ptr, size);
  233. return ptr;
  234. }
  235. unsigned long msm_iomap_size(struct platform_device *pdev, const char *name)
  236. {
  237. struct resource *res;
  238. if (name)
  239. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  240. else
  241. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  242. if (!res) {
  243. dev_dbg(&pdev->dev, "failed to get memory resource: %s\n",
  244. name);
  245. return 0;
  246. }
  247. return resource_size(res);
  248. }
  249. void msm_iounmap(struct platform_device *pdev, void __iomem *addr)
  250. {
  251. devm_iounmap(&pdev->dev, addr);
  252. }
  253. void msm_writel(u32 data, void __iomem *addr)
  254. {
  255. if (reglog)
  256. pr_debug("IO:W %pK %08x\n", addr, data);
  257. writel(data, addr);
  258. }
  259. u32 msm_readl(const void __iomem *addr)
  260. {
  261. u32 val = readl(addr);
  262. if (reglog)
  263. pr_err("IO:R %pK %08x\n", addr, val);
  264. return val;
  265. }
  266. int msm_get_src_bpc(int chroma_format,
  267. int bpc)
  268. {
  269. int src_bpp;
  270. switch (chroma_format) {
  271. case MSM_CHROMA_444:
  272. src_bpp = bpc * 3;
  273. break;
  274. case MSM_CHROMA_422:
  275. src_bpp = bpc * 2;
  276. break;
  277. case MSM_CHROMA_420:
  278. src_bpp = mult_frac(bpc, 3, 2);
  279. break;
  280. default:
  281. src_bpp = bpc * 3;
  282. break;
  283. }
  284. return src_bpp;
  285. }
  286. struct vblank_work {
  287. struct kthread_work work;
  288. int crtc_id;
  289. bool enable;
  290. struct msm_drm_private *priv;
  291. };
  292. static void vblank_ctrl_worker(struct kthread_work *work)
  293. {
  294. struct vblank_work *cur_work = container_of(work,
  295. struct vblank_work, work);
  296. struct msm_drm_private *priv = cur_work->priv;
  297. struct msm_kms *kms = priv->kms;
  298. if (cur_work->enable)
  299. kms->funcs->enable_vblank(kms, priv->crtcs[cur_work->crtc_id]);
  300. else
  301. kms->funcs->disable_vblank(kms, priv->crtcs[cur_work->crtc_id]);
  302. kfree(cur_work);
  303. }
  304. static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
  305. int crtc_id, bool enable)
  306. {
  307. struct vblank_work *cur_work;
  308. struct drm_crtc *crtc;
  309. struct kthread_worker *worker;
  310. if (!priv || crtc_id >= priv->num_crtcs)
  311. return -EINVAL;
  312. cur_work = kzalloc(sizeof(*cur_work), GFP_ATOMIC);
  313. if (!cur_work)
  314. return -ENOMEM;
  315. crtc = priv->crtcs[crtc_id];
  316. kthread_init_work(&cur_work->work, vblank_ctrl_worker);
  317. cur_work->crtc_id = crtc_id;
  318. cur_work->enable = enable;
  319. cur_work->priv = priv;
  320. worker = &priv->event_thread[crtc_id].worker;
  321. kthread_queue_work(worker, &cur_work->work);
  322. return 0;
  323. }
  324. static int msm_drm_uninit(struct device *dev)
  325. {
  326. struct platform_device *pdev = to_platform_device(dev);
  327. struct drm_device *ddev = platform_get_drvdata(pdev);
  328. struct msm_drm_private *priv = ddev->dev_private;
  329. struct msm_kms *kms = priv->kms;
  330. struct msm_vm_client_entry *client_entry, *tmp;
  331. int i;
  332. flush_workqueue(priv->wq);
  333. pm_runtime_get_sync(dev);
  334. /* clean up display commit/event worker threads */
  335. for (i = 0; i < priv->num_crtcs; i++) {
  336. if (priv->disp_thread[i].thread) {
  337. kthread_flush_worker(&priv->disp_thread[i].worker);
  338. kthread_stop(priv->disp_thread[i].thread);
  339. priv->disp_thread[i].thread = NULL;
  340. }
  341. if (priv->event_thread[i].thread) {
  342. kthread_flush_worker(&priv->event_thread[i].worker);
  343. kthread_stop(priv->event_thread[i].thread);
  344. priv->event_thread[i].thread = NULL;
  345. }
  346. }
  347. drm_kms_helper_poll_fini(ddev);
  348. if (kms && kms->funcs)
  349. kms->funcs->debugfs_destroy(kms);
  350. sde_dbg_destroy();
  351. debugfs_remove_recursive(priv->debug_root);
  352. drm_mode_config_cleanup(ddev);
  353. if (priv->registered) {
  354. drm_dev_unregister(ddev);
  355. priv->registered = false;
  356. }
  357. #ifdef CONFIG_DRM_FBDEV_EMULATION
  358. if (fbdev && priv->fbdev)
  359. msm_fbdev_free(ddev);
  360. #endif
  361. drm_atomic_helper_shutdown(ddev);
  362. drm_irq_uninstall(ddev);
  363. if (kms && kms->funcs)
  364. kms->funcs->destroy(kms);
  365. if (priv->vram.paddr) {
  366. unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
  367. drm_mm_takedown(&priv->vram.mm);
  368. dma_free_attrs(dev, priv->vram.size, NULL,
  369. priv->vram.paddr, attrs);
  370. }
  371. component_unbind_all(dev, ddev);
  372. pm_runtime_put_sync(dev);
  373. sde_power_resource_deinit(pdev, &priv->phandle);
  374. mutex_lock(&priv->vm_client_lock);
  375. /* clean up any unregistered clients */
  376. list_for_each_entry_safe(client_entry, tmp, &priv->vm_client_list,
  377. list) {
  378. list_del(&client_entry->list);
  379. kfree(client_entry);
  380. }
  381. mutex_unlock(&priv->vm_client_lock);
  382. msm_mdss_destroy(ddev);
  383. ddev->dev_private = NULL;
  384. destroy_workqueue(priv->wq);
  385. kfree(priv);
  386. drm_dev_put(ddev);
  387. return 0;
  388. }
  389. #define KMS_MDP4 4
  390. #define KMS_MDP5 5
  391. #define KMS_SDE 3
  392. static int get_mdp_ver(struct platform_device *pdev)
  393. {
  394. #ifdef CONFIG_OF
  395. static const struct of_device_id match_types[] = { {
  396. .compatible = "qcom,mdss_mdp",
  397. .data = (void *)KMS_MDP5,
  398. },
  399. {
  400. .compatible = "qcom,sde-kms",
  401. .data = (void *)KMS_SDE,
  402. },
  403. {},
  404. };
  405. struct device *dev = &pdev->dev;
  406. const struct of_device_id *match;
  407. match = of_match_node(match_types, dev->of_node);
  408. if (match)
  409. return (int)(unsigned long)match->data;
  410. #endif
  411. return KMS_MDP4;
  412. }
  413. static int msm_init_vram(struct drm_device *dev)
  414. {
  415. struct msm_drm_private *priv = dev->dev_private;
  416. struct device_node *node;
  417. unsigned long size = 0;
  418. int ret = 0;
  419. /* In the device-tree world, we could have a 'memory-region'
  420. * phandle, which gives us a link to our "vram". Allocating
  421. * is all nicely abstracted behind the dma api, but we need
  422. * to know the entire size to allocate it all in one go. There
  423. * are two cases:
  424. * 1) device with no IOMMU, in which case we need exclusive
  425. * access to a VRAM carveout big enough for all gpu
  426. * buffers
  427. * 2) device with IOMMU, but where the bootloader puts up
  428. * a splash screen. In this case, the VRAM carveout
  429. * need only be large enough for fbdev fb. But we need
  430. * exclusive access to the buffer to avoid the kernel
  431. * using those pages for other purposes (which appears
  432. * as corruption on screen before we have a chance to
  433. * load and do initial modeset)
  434. */
  435. node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
  436. if (node) {
  437. struct resource r;
  438. ret = of_address_to_resource(node, 0, &r);
  439. of_node_put(node);
  440. if (ret)
  441. return ret;
  442. size = r.end - r.start;
  443. DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
  444. /* if we have no IOMMU, then we need to use carveout allocator.
  445. * Grab the entire CMA chunk carved out in early startup in
  446. * mach-msm:
  447. */
  448. } else if (!iommu_present(&platform_bus_type)) {
  449. u32 vram_size;
  450. ret = of_property_read_u32(dev->dev->of_node,
  451. "qcom,vram-size", &vram_size);
  452. size = (ret < 0) ? memparse(vram, NULL) : vram_size;
  453. DRM_INFO("using 0x%x VRAM carveout\n", size);
  454. ret = 0;
  455. }
  456. if (size) {
  457. unsigned long attrs = 0;
  458. void *p;
  459. priv->vram.size = size;
  460. drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
  461. spin_lock_init(&priv->vram.lock);
  462. attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
  463. attrs |= DMA_ATTR_WRITE_COMBINE;
  464. /* note that for no-kernel-mapping, the vaddr returned
  465. * is bogus, but non-null if allocation succeeded:
  466. */
  467. p = dma_alloc_attrs(dev->dev, size,
  468. &priv->vram.paddr, GFP_KERNEL, attrs);
  469. if (!p) {
  470. dev_err(dev->dev, "failed to allocate VRAM\n");
  471. priv->vram.paddr = 0;
  472. return -ENOMEM;
  473. }
  474. dev_info(dev->dev, "VRAM: %08x->%08x\n",
  475. (uint32_t)priv->vram.paddr,
  476. (uint32_t)(priv->vram.paddr + size));
  477. }
  478. return ret;
  479. }
  480. #ifdef CONFIG_OF
  481. static int msm_component_bind_all(struct device *dev,
  482. struct drm_device *drm_dev)
  483. {
  484. int ret;
  485. ret = component_bind_all(dev, drm_dev);
  486. if (ret)
  487. DRM_ERROR("component_bind_all failed: %d\n", ret);
  488. return ret;
  489. }
  490. #else
  491. static int msm_component_bind_all(struct device *dev,
  492. struct drm_device *drm_dev)
  493. {
  494. return 0;
  495. }
  496. #endif
  497. static int msm_drm_display_thread_create(struct sched_param param,
  498. struct msm_drm_private *priv, struct drm_device *ddev,
  499. struct device *dev)
  500. {
  501. int i, ret = 0;
  502. /**
  503. * this priority was found during empiric testing to have appropriate
  504. * realtime scheduling to process display updates and interact with
  505. * other real time and normal priority task
  506. */
  507. param.sched_priority = 16;
  508. for (i = 0; i < priv->num_crtcs; i++) {
  509. /* initialize display thread */
  510. priv->disp_thread[i].crtc_id = priv->crtcs[i]->base.id;
  511. kthread_init_worker(&priv->disp_thread[i].worker);
  512. priv->disp_thread[i].dev = ddev;
  513. priv->disp_thread[i].thread =
  514. kthread_run(kthread_worker_fn,
  515. &priv->disp_thread[i].worker,
  516. "crtc_commit:%d", priv->disp_thread[i].crtc_id);
  517. ret = sched_setscheduler(priv->disp_thread[i].thread,
  518. SCHED_FIFO, &param);
  519. if (ret)
  520. pr_warn("display thread priority update failed: %d\n",
  521. ret);
  522. if (IS_ERR(priv->disp_thread[i].thread)) {
  523. dev_err(dev, "failed to create crtc_commit kthread\n");
  524. priv->disp_thread[i].thread = NULL;
  525. }
  526. /* initialize event thread */
  527. priv->event_thread[i].crtc_id = priv->crtcs[i]->base.id;
  528. kthread_init_worker(&priv->event_thread[i].worker);
  529. priv->event_thread[i].dev = ddev;
  530. priv->event_thread[i].thread =
  531. kthread_run(kthread_worker_fn,
  532. &priv->event_thread[i].worker,
  533. "crtc_event:%d", priv->event_thread[i].crtc_id);
  534. /**
  535. * event thread should also run at same priority as disp_thread
  536. * because it is handling frame_done events. A lower priority
  537. * event thread and higher priority disp_thread can causes
  538. * frame_pending counters beyond 2. This can lead to commit
  539. * failure at crtc commit level.
  540. */
  541. ret = sched_setscheduler(priv->event_thread[i].thread,
  542. SCHED_FIFO, &param);
  543. if (ret)
  544. pr_warn("display event thread priority update failed: %d\n",
  545. ret);
  546. if (IS_ERR(priv->event_thread[i].thread)) {
  547. dev_err(dev, "failed to create crtc_event kthread\n");
  548. priv->event_thread[i].thread = NULL;
  549. }
  550. if ((!priv->disp_thread[i].thread) ||
  551. !priv->event_thread[i].thread) {
  552. /* clean up previously created threads if any */
  553. for ( ; i >= 0; i--) {
  554. if (priv->disp_thread[i].thread) {
  555. kthread_stop(
  556. priv->disp_thread[i].thread);
  557. priv->disp_thread[i].thread = NULL;
  558. }
  559. if (priv->event_thread[i].thread) {
  560. kthread_stop(
  561. priv->event_thread[i].thread);
  562. priv->event_thread[i].thread = NULL;
  563. }
  564. }
  565. return -EINVAL;
  566. }
  567. }
  568. /**
  569. * Since pp interrupt is heavy weight, try to queue the work
  570. * into a dedicated worker thread, so that they dont interrupt
  571. * other important events.
  572. */
  573. kthread_init_worker(&priv->pp_event_worker);
  574. priv->pp_event_thread = kthread_run(kthread_worker_fn,
  575. &priv->pp_event_worker, "pp_event");
  576. ret = sched_setscheduler(priv->pp_event_thread,
  577. SCHED_FIFO, &param);
  578. if (ret)
  579. pr_warn("pp_event thread priority update failed: %d\n",
  580. ret);
  581. if (IS_ERR(priv->pp_event_thread)) {
  582. dev_err(dev, "failed to create pp_event kthread\n");
  583. ret = PTR_ERR(priv->pp_event_thread);
  584. priv->pp_event_thread = NULL;
  585. return ret;
  586. }
  587. return 0;
  588. }
  589. static struct msm_kms *_msm_drm_component_init_helper(
  590. struct msm_drm_private *priv,
  591. struct drm_device *ddev, struct device *dev,
  592. struct platform_device *pdev)
  593. {
  594. int ret;
  595. struct msm_kms *kms;
  596. switch (get_mdp_ver(pdev)) {
  597. case KMS_MDP4:
  598. kms = mdp4_kms_init(ddev);
  599. break;
  600. case KMS_MDP5:
  601. kms = mdp5_kms_init(ddev);
  602. break;
  603. case KMS_SDE:
  604. kms = sde_kms_init(ddev);
  605. break;
  606. default:
  607. kms = ERR_PTR(-ENODEV);
  608. break;
  609. }
  610. if (IS_ERR_OR_NULL(kms)) {
  611. /*
  612. * NOTE: once we have GPU support, having no kms should not
  613. * be considered fatal.. ideally we would still support gpu
  614. * and (for example) use dmabuf/prime to share buffers with
  615. * imx drm driver on iMX5
  616. */
  617. dev_err(dev, "failed to load kms\n");
  618. return kms;
  619. }
  620. priv->kms = kms;
  621. /**
  622. * Since kms->funcs->hw_init(kms) might call
  623. * drm_object_property_set_value to initialize some custom
  624. * properties we need to make sure mode_config.funcs are populated
  625. * beforehand to avoid dereferencing an unset value during the
  626. * drm_drv_uses_atomic_modeset check.
  627. */
  628. ddev->mode_config.funcs = &mode_config_funcs;
  629. ret = (kms)->funcs->hw_init(kms);
  630. if (ret) {
  631. dev_err(dev, "kms hw init failed: %d\n", ret);
  632. return ERR_PTR(ret);
  633. }
  634. return kms;
  635. }
  636. static int msm_drm_device_init(struct platform_device *pdev,
  637. struct drm_driver *drv)
  638. {
  639. struct device *dev = &pdev->dev;
  640. struct drm_device *ddev;
  641. struct msm_drm_private *priv;
  642. int i, ret;
  643. ddev = drm_dev_alloc(drv, dev);
  644. if (IS_ERR(ddev)) {
  645. dev_err(dev, "failed to allocate drm_device\n");
  646. return PTR_ERR(ddev);
  647. }
  648. drm_mode_config_init(ddev);
  649. platform_set_drvdata(pdev, ddev);
  650. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  651. if (!priv) {
  652. ret = -ENOMEM;
  653. goto priv_alloc_fail;
  654. }
  655. ddev->dev_private = priv;
  656. priv->dev = ddev;
  657. ret = sde_power_resource_init(pdev, &priv->phandle);
  658. if (ret) {
  659. pr_err("sde power resource init failed\n");
  660. goto power_init_fail;
  661. }
  662. ret = sde_dbg_init(&pdev->dev);
  663. if (ret) {
  664. dev_err(dev, "failed to init sde dbg: %d\n", ret);
  665. goto dbg_init_fail;
  666. }
  667. pm_runtime_enable(dev);
  668. ret = pm_runtime_get_sync(dev);
  669. if (ret < 0) {
  670. dev_err(dev, "resource enable failed: %d\n", ret);
  671. goto pm_runtime_error;
  672. }
  673. for (i = 0; i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++)
  674. sde_power_data_bus_set_quota(&priv->phandle, i,
  675. SDE_POWER_HANDLE_CONT_SPLASH_BUS_AB_QUOTA,
  676. SDE_POWER_HANDLE_CONT_SPLASH_BUS_IB_QUOTA);
  677. return ret;
  678. pm_runtime_error:
  679. sde_dbg_destroy();
  680. dbg_init_fail:
  681. sde_power_resource_deinit(pdev, &priv->phandle);
  682. power_init_fail:
  683. priv_alloc_fail:
  684. drm_dev_put(ddev);
  685. kfree(priv);
  686. return ret;
  687. }
  688. static int msm_drm_component_init(struct device *dev)
  689. {
  690. struct platform_device *pdev = to_platform_device(dev);
  691. struct drm_device *ddev = platform_get_drvdata(pdev);
  692. struct msm_drm_private *priv = ddev->dev_private;
  693. struct msm_kms *kms = NULL;
  694. int ret;
  695. struct sched_param param = { 0 };
  696. struct drm_crtc *crtc;
  697. ret = msm_mdss_init(ddev);
  698. if (ret)
  699. goto mdss_init_fail;
  700. priv->wq = alloc_ordered_workqueue("msm_drm", 0);
  701. init_waitqueue_head(&priv->pending_crtcs_event);
  702. INIT_LIST_HEAD(&priv->client_event_list);
  703. INIT_LIST_HEAD(&priv->inactive_list);
  704. INIT_LIST_HEAD(&priv->vm_client_list);
  705. mutex_init(&priv->vm_client_lock);
  706. /* Bind all our sub-components: */
  707. ret = msm_component_bind_all(dev, ddev);
  708. if (ret)
  709. goto bind_fail;
  710. ret = msm_init_vram(ddev);
  711. if (ret)
  712. goto fail;
  713. ddev->mode_config.funcs = &mode_config_funcs;
  714. ddev->mode_config.helper_private = &mode_config_helper_funcs;
  715. kms = _msm_drm_component_init_helper(priv, ddev, dev, pdev);
  716. if (IS_ERR_OR_NULL(kms)) {
  717. dev_err(dev, "msm_drm_component_init_helper failed\n");
  718. goto fail;
  719. }
  720. ret = msm_drm_display_thread_create(param, priv, ddev, dev);
  721. if (ret) {
  722. dev_err(dev, "msm_drm_display_thread_create failed\n");
  723. goto fail;
  724. }
  725. ret = drm_vblank_init(ddev, priv->num_crtcs);
  726. if (ret < 0) {
  727. dev_err(dev, "failed to initialize vblank\n");
  728. goto fail;
  729. }
  730. drm_for_each_crtc(crtc, ddev)
  731. drm_crtc_vblank_reset(crtc);
  732. if (kms) {
  733. pm_runtime_get_sync(dev);
  734. ret = drm_irq_install(ddev, platform_get_irq(pdev, 0));
  735. pm_runtime_put_sync(dev);
  736. if (ret < 0) {
  737. dev_err(dev, "failed to install IRQ handler\n");
  738. goto fail;
  739. }
  740. }
  741. ret = drm_dev_register(ddev, 0);
  742. if (ret)
  743. goto fail;
  744. priv->registered = true;
  745. drm_mode_config_reset(ddev);
  746. if (kms && kms->funcs && kms->funcs->cont_splash_config) {
  747. ret = kms->funcs->cont_splash_config(kms);
  748. if (ret) {
  749. dev_err(dev, "kms cont_splash config failed.\n");
  750. goto fail;
  751. }
  752. }
  753. #ifdef CONFIG_DRM_FBDEV_EMULATION
  754. if (fbdev)
  755. priv->fbdev = msm_fbdev_init(ddev);
  756. #endif
  757. /* create drm client only when fbdev is not supported */
  758. if (!priv->fbdev) {
  759. ret = drm_client_init(ddev, &kms->client, "kms_client", NULL);
  760. if (ret) {
  761. DRM_ERROR("failed to init kms_client: %d\n", ret);
  762. kms->client.dev = NULL;
  763. goto fail;
  764. }
  765. drm_client_register(&kms->client);
  766. }
  767. ret = sde_dbg_debugfs_register(dev);
  768. if (ret) {
  769. dev_err(dev, "failed to reg sde dbg debugfs: %d\n", ret);
  770. goto fail;
  771. }
  772. /* perform subdriver post initialization */
  773. if (kms && kms->funcs && kms->funcs->postinit) {
  774. ret = kms->funcs->postinit(kms);
  775. if (ret) {
  776. pr_err("kms post init failed: %d\n", ret);
  777. goto fail;
  778. }
  779. }
  780. drm_kms_helper_poll_init(ddev);
  781. return 0;
  782. fail:
  783. msm_drm_uninit(dev);
  784. return ret;
  785. bind_fail:
  786. msm_mdss_destroy(ddev);
  787. mdss_init_fail:
  788. sde_dbg_destroy();
  789. sde_power_resource_deinit(pdev, &priv->phandle);
  790. drm_dev_put(ddev);
  791. kfree(priv);
  792. return ret;
  793. }
  794. /*
  795. * DRM operations:
  796. */
  797. static int context_init(struct drm_device *dev, struct drm_file *file)
  798. {
  799. struct msm_file_private *ctx;
  800. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  801. if (!ctx)
  802. return -ENOMEM;
  803. mutex_init(&ctx->power_lock);
  804. file->driver_priv = ctx;
  805. if (dev && dev->dev_private) {
  806. struct msm_drm_private *priv = dev->dev_private;
  807. struct msm_kms *kms;
  808. kms = priv->kms;
  809. if (kms && kms->funcs && kms->funcs->postopen)
  810. kms->funcs->postopen(kms, file);
  811. }
  812. return 0;
  813. }
  814. static int msm_open(struct drm_device *dev, struct drm_file *file)
  815. {
  816. return context_init(dev, file);
  817. }
  818. static void context_close(struct msm_file_private *ctx)
  819. {
  820. kfree(ctx);
  821. }
  822. static void msm_postclose(struct drm_device *dev, struct drm_file *file)
  823. {
  824. struct msm_drm_private *priv = dev->dev_private;
  825. struct msm_file_private *ctx = file->driver_priv;
  826. struct msm_kms *kms = priv->kms;
  827. if (!kms)
  828. return;
  829. if (kms->funcs && kms->funcs->postclose)
  830. kms->funcs->postclose(kms, file);
  831. mutex_lock(&dev->struct_mutex);
  832. if (ctx == priv->lastctx)
  833. priv->lastctx = NULL;
  834. mutex_unlock(&dev->struct_mutex);
  835. mutex_lock(&ctx->power_lock);
  836. if (ctx->enable_refcnt) {
  837. SDE_EVT32(ctx->enable_refcnt);
  838. pm_runtime_put_sync(dev->dev);
  839. }
  840. mutex_unlock(&ctx->power_lock);
  841. context_close(ctx);
  842. }
  843. static void msm_lastclose(struct drm_device *dev)
  844. {
  845. struct msm_drm_private *priv = dev->dev_private;
  846. struct msm_kms *kms = priv->kms;
  847. int i, rc;
  848. if (!kms)
  849. return;
  850. /* check for splash status before triggering cleanup
  851. * if we end up here with splash status ON i.e before first
  852. * commit then ignore the last close call
  853. */
  854. if (kms->funcs && kms->funcs->check_for_splash
  855. && kms->funcs->check_for_splash(kms))
  856. return;
  857. /*
  858. * clean up vblank disable immediately as this is the last close.
  859. */
  860. for (i = 0; i < dev->num_crtcs; i++) {
  861. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  862. struct timer_list *disable_timer = &vblank->disable_timer;
  863. if (del_timer_sync(disable_timer))
  864. disable_timer->function(disable_timer);
  865. }
  866. /* wait for pending vblank requests to be executed by worker thread */
  867. flush_workqueue(priv->wq);
  868. /* wait for any pending crtcs to finish before lastclose commit */
  869. msm_wait_event_timeout(priv->pending_crtcs_event, !priv->pending_crtcs,
  870. LASTCLOSE_TIMEOUT_MS, rc);
  871. if (!rc)
  872. DRM_INFO("wait for crtc mask 0x%x failed, commit anyway...\n",
  873. priv->pending_crtcs);
  874. if (priv->fbdev) {
  875. rc = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  876. if (rc)
  877. DRM_ERROR("restore FBDEV mode failed: %d\n", rc);
  878. } else if (kms && kms->client.dev) {
  879. rc = drm_client_modeset_commit_force(&kms->client);
  880. if (rc)
  881. DRM_ERROR("client modeset commit failed: %d\n", rc);
  882. }
  883. /* wait again, before kms driver does it's lastclose commit */
  884. msm_wait_event_timeout(priv->pending_crtcs_event, !priv->pending_crtcs,
  885. LASTCLOSE_TIMEOUT_MS, rc);
  886. if (!rc)
  887. DRM_INFO("wait for crtc mask 0x%x failed, commit anyway...\n",
  888. priv->pending_crtcs);
  889. if (kms->funcs && kms->funcs->lastclose)
  890. kms->funcs->lastclose(kms);
  891. }
  892. static irqreturn_t msm_irq(int irq, void *arg)
  893. {
  894. struct drm_device *dev = arg;
  895. struct msm_drm_private *priv = dev->dev_private;
  896. struct msm_kms *kms = priv->kms;
  897. BUG_ON(!kms);
  898. return kms->funcs->irq(kms);
  899. }
  900. static void msm_irq_preinstall(struct drm_device *dev)
  901. {
  902. struct msm_drm_private *priv = dev->dev_private;
  903. struct msm_kms *kms = priv->kms;
  904. BUG_ON(!kms);
  905. kms->funcs->irq_preinstall(kms);
  906. }
  907. static int msm_irq_postinstall(struct drm_device *dev)
  908. {
  909. struct msm_drm_private *priv = dev->dev_private;
  910. struct msm_kms *kms = priv->kms;
  911. BUG_ON(!kms);
  912. if (kms->funcs->irq_postinstall)
  913. return kms->funcs->irq_postinstall(kms);
  914. return 0;
  915. }
  916. static void msm_irq_uninstall(struct drm_device *dev)
  917. {
  918. struct msm_drm_private *priv = dev->dev_private;
  919. struct msm_kms *kms = priv->kms;
  920. BUG_ON(!kms);
  921. kms->funcs->irq_uninstall(kms);
  922. }
  923. static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
  924. {
  925. struct msm_drm_private *priv = dev->dev_private;
  926. struct msm_kms *kms = priv->kms;
  927. if (!kms)
  928. return -ENXIO;
  929. DBG("dev=%pK, crtc=%u", dev, pipe);
  930. return vblank_ctrl_queue_work(priv, pipe, true);
  931. }
  932. static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
  933. {
  934. struct msm_drm_private *priv = dev->dev_private;
  935. struct msm_kms *kms = priv->kms;
  936. if (!kms)
  937. return;
  938. DBG("dev=%pK, crtc=%u", dev, pipe);
  939. vblank_ctrl_queue_work(priv, pipe, false);
  940. }
  941. /*
  942. * DRM ioctls:
  943. */
  944. static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
  945. struct drm_file *file)
  946. {
  947. struct drm_msm_gem_new *args = data;
  948. if (args->flags & ~MSM_BO_FLAGS) {
  949. DRM_ERROR("invalid flags: %08x\n", args->flags);
  950. return -EINVAL;
  951. }
  952. return msm_gem_new_handle(dev, file, args->size,
  953. args->flags, &args->handle, NULL);
  954. }
  955. static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
  956. {
  957. return ktime_set(timeout.tv_sec, timeout.tv_nsec);
  958. }
  959. static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  960. struct drm_file *file)
  961. {
  962. struct drm_msm_gem_cpu_prep *args = data;
  963. struct drm_gem_object *obj;
  964. ktime_t timeout = to_ktime(args->timeout);
  965. int ret;
  966. if (args->op & ~MSM_PREP_FLAGS) {
  967. DRM_ERROR("invalid op: %08x\n", args->op);
  968. return -EINVAL;
  969. }
  970. obj = drm_gem_object_lookup(file, args->handle);
  971. if (!obj)
  972. return -ENOENT;
  973. ret = msm_gem_cpu_prep(obj, args->op, &timeout);
  974. drm_gem_object_put_unlocked(obj);
  975. return ret;
  976. }
  977. static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  978. struct drm_file *file)
  979. {
  980. struct drm_msm_gem_cpu_fini *args = data;
  981. struct drm_gem_object *obj;
  982. int ret;
  983. obj = drm_gem_object_lookup(file, args->handle);
  984. if (!obj)
  985. return -ENOENT;
  986. ret = msm_gem_cpu_fini(obj);
  987. drm_gem_object_put_unlocked(obj);
  988. return ret;
  989. }
  990. static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
  991. struct drm_file *file)
  992. {
  993. struct drm_msm_gem_madvise *args = data;
  994. struct drm_gem_object *obj;
  995. int ret;
  996. switch (args->madv) {
  997. case MSM_MADV_DONTNEED:
  998. case MSM_MADV_WILLNEED:
  999. break;
  1000. default:
  1001. return -EINVAL;
  1002. }
  1003. ret = mutex_lock_interruptible(&dev->struct_mutex);
  1004. if (ret)
  1005. return ret;
  1006. obj = drm_gem_object_lookup(file, args->handle);
  1007. if (!obj) {
  1008. ret = -ENOENT;
  1009. goto unlock;
  1010. }
  1011. ret = msm_gem_madvise(obj, args->madv);
  1012. if (ret >= 0) {
  1013. args->retained = ret;
  1014. ret = 0;
  1015. }
  1016. drm_gem_object_put(obj);
  1017. unlock:
  1018. mutex_unlock(&dev->struct_mutex);
  1019. return ret;
  1020. }
  1021. static int msm_drm_object_supports_event(struct drm_device *dev,
  1022. struct drm_msm_event_req *req)
  1023. {
  1024. int ret = -EINVAL;
  1025. struct drm_mode_object *arg_obj;
  1026. arg_obj = drm_mode_object_find(dev, NULL, req->object_id,
  1027. req->object_type);
  1028. if (!arg_obj)
  1029. return -ENOENT;
  1030. switch (arg_obj->type) {
  1031. case DRM_MODE_OBJECT_CRTC:
  1032. case DRM_MODE_OBJECT_CONNECTOR:
  1033. ret = 0;
  1034. break;
  1035. default:
  1036. ret = -EOPNOTSUPP;
  1037. break;
  1038. }
  1039. drm_mode_object_put(arg_obj);
  1040. return ret;
  1041. }
  1042. static int msm_register_event(struct drm_device *dev,
  1043. struct drm_msm_event_req *req, struct drm_file *file, bool en)
  1044. {
  1045. int ret = -EINVAL;
  1046. struct msm_drm_private *priv = dev->dev_private;
  1047. struct msm_kms *kms = priv->kms;
  1048. struct drm_mode_object *arg_obj;
  1049. arg_obj = drm_mode_object_find(dev, file, req->object_id,
  1050. req->object_type);
  1051. if (!arg_obj)
  1052. return -ENOENT;
  1053. ret = kms->funcs->register_events(kms, arg_obj, req->event, en);
  1054. drm_mode_object_put(arg_obj);
  1055. return ret;
  1056. }
  1057. static int msm_event_client_count(struct drm_device *dev,
  1058. struct drm_msm_event_req *req_event, bool locked)
  1059. {
  1060. struct msm_drm_private *priv = dev->dev_private;
  1061. unsigned long flag = 0;
  1062. struct msm_drm_event *node;
  1063. int count = 0;
  1064. if (!locked)
  1065. spin_lock_irqsave(&dev->event_lock, flag);
  1066. list_for_each_entry(node, &priv->client_event_list, base.link) {
  1067. if (node->event.base.type == req_event->event &&
  1068. node->event.info.object_id == req_event->object_id)
  1069. count++;
  1070. }
  1071. if (!locked)
  1072. spin_unlock_irqrestore(&dev->event_lock, flag);
  1073. return count;
  1074. }
  1075. static int msm_ioctl_register_event(struct drm_device *dev, void *data,
  1076. struct drm_file *file)
  1077. {
  1078. struct msm_drm_private *priv = dev->dev_private;
  1079. struct drm_msm_event_req *req_event = data;
  1080. struct msm_drm_event *client, *node;
  1081. unsigned long flag = 0;
  1082. bool dup_request = false;
  1083. int ret = 0, count = 0;
  1084. ret = msm_drm_object_supports_event(dev, req_event);
  1085. if (ret) {
  1086. DRM_ERROR("unsupported event %x object %x object id %d\n",
  1087. req_event->event, req_event->object_type,
  1088. req_event->object_id);
  1089. return ret;
  1090. }
  1091. spin_lock_irqsave(&dev->event_lock, flag);
  1092. list_for_each_entry(node, &priv->client_event_list, base.link) {
  1093. if (node->base.file_priv != file)
  1094. continue;
  1095. if (node->event.base.type == req_event->event &&
  1096. node->event.info.object_id == req_event->object_id) {
  1097. DRM_DEBUG("duplicate request for event %x obj id %d\n",
  1098. node->event.base.type,
  1099. node->event.info.object_id);
  1100. dup_request = true;
  1101. break;
  1102. }
  1103. }
  1104. spin_unlock_irqrestore(&dev->event_lock, flag);
  1105. if (dup_request)
  1106. return -EALREADY;
  1107. client = kzalloc(sizeof(*client), GFP_KERNEL);
  1108. if (!client)
  1109. return -ENOMEM;
  1110. client->base.file_priv = file;
  1111. client->base.event = &client->event.base;
  1112. client->event.base.type = req_event->event;
  1113. memcpy(&client->event.info, req_event, sizeof(client->event.info));
  1114. /* Get the count of clients that have registered for event.
  1115. * Event should be enabled for first client, for subsequent enable
  1116. * calls add to client list and return.
  1117. */
  1118. count = msm_event_client_count(dev, req_event, false);
  1119. /* Add current client to list */
  1120. spin_lock_irqsave(&dev->event_lock, flag);
  1121. list_add_tail(&client->base.link, &priv->client_event_list);
  1122. spin_unlock_irqrestore(&dev->event_lock, flag);
  1123. if (count)
  1124. return 0;
  1125. ret = msm_register_event(dev, req_event, file, true);
  1126. if (ret) {
  1127. DRM_ERROR("failed to enable event %x object %x object id %d\n",
  1128. req_event->event, req_event->object_type,
  1129. req_event->object_id);
  1130. spin_lock_irqsave(&dev->event_lock, flag);
  1131. list_del(&client->base.link);
  1132. spin_unlock_irqrestore(&dev->event_lock, flag);
  1133. kfree(client);
  1134. }
  1135. return ret;
  1136. }
  1137. static int msm_ioctl_deregister_event(struct drm_device *dev, void *data,
  1138. struct drm_file *file)
  1139. {
  1140. struct msm_drm_private *priv = dev->dev_private;
  1141. struct drm_msm_event_req *req_event = data;
  1142. struct msm_drm_event *client = NULL, *node, *temp;
  1143. unsigned long flag = 0;
  1144. int count = 0;
  1145. bool found = false;
  1146. int ret = 0;
  1147. ret = msm_drm_object_supports_event(dev, req_event);
  1148. if (ret) {
  1149. DRM_ERROR("unsupported event %x object %x object id %d\n",
  1150. req_event->event, req_event->object_type,
  1151. req_event->object_id);
  1152. return ret;
  1153. }
  1154. spin_lock_irqsave(&dev->event_lock, flag);
  1155. list_for_each_entry_safe(node, temp, &priv->client_event_list,
  1156. base.link) {
  1157. if (node->event.base.type == req_event->event &&
  1158. node->event.info.object_id == req_event->object_id &&
  1159. node->base.file_priv == file) {
  1160. client = node;
  1161. list_del(&client->base.link);
  1162. found = true;
  1163. kfree(client);
  1164. break;
  1165. }
  1166. }
  1167. spin_unlock_irqrestore(&dev->event_lock, flag);
  1168. if (!found)
  1169. return -ENOENT;
  1170. count = msm_event_client_count(dev, req_event, false);
  1171. if (!count)
  1172. ret = msm_register_event(dev, req_event, file, false);
  1173. return ret;
  1174. }
  1175. void msm_mode_object_event_notify(struct drm_mode_object *obj,
  1176. struct drm_device *dev, struct drm_event *event, u8 *payload)
  1177. {
  1178. struct msm_drm_private *priv = NULL;
  1179. unsigned long flags;
  1180. struct msm_drm_event *notify, *node;
  1181. int len = 0, ret;
  1182. if (!obj || !event || !event->length || !payload) {
  1183. DRM_ERROR("err param obj %pK event %pK len %d payload %pK\n",
  1184. obj, event, ((event) ? (event->length) : -1),
  1185. payload);
  1186. return;
  1187. }
  1188. priv = (dev) ? dev->dev_private : NULL;
  1189. if (!dev || !priv) {
  1190. DRM_ERROR("invalid dev %pK priv %pK\n", dev, priv);
  1191. return;
  1192. }
  1193. spin_lock_irqsave(&dev->event_lock, flags);
  1194. list_for_each_entry(node, &priv->client_event_list, base.link) {
  1195. if (node->event.base.type != event->type ||
  1196. obj->id != node->event.info.object_id)
  1197. continue;
  1198. len = event->length + sizeof(struct msm_drm_event);
  1199. if (node->base.file_priv->event_space < len) {
  1200. DRM_ERROR("Insufficient space %d for event %x len %d\n",
  1201. node->base.file_priv->event_space, event->type,
  1202. len);
  1203. continue;
  1204. }
  1205. notify = kzalloc(len, GFP_ATOMIC);
  1206. if (!notify)
  1207. continue;
  1208. notify->base.file_priv = node->base.file_priv;
  1209. notify->base.event = &notify->event.base;
  1210. notify->event.base.type = node->event.base.type;
  1211. notify->event.base.length = event->length +
  1212. sizeof(struct drm_msm_event_resp);
  1213. memcpy(&notify->event.info, &node->event.info,
  1214. sizeof(notify->event.info));
  1215. memcpy(notify->event.data, payload, event->length);
  1216. ret = drm_event_reserve_init_locked(dev, node->base.file_priv,
  1217. &notify->base, &notify->event.base);
  1218. if (ret) {
  1219. kfree(notify);
  1220. continue;
  1221. }
  1222. drm_send_event_locked(dev, &notify->base);
  1223. }
  1224. spin_unlock_irqrestore(&dev->event_lock, flags);
  1225. }
  1226. static int msm_release(struct inode *inode, struct file *filp)
  1227. {
  1228. struct drm_file *file_priv = filp->private_data;
  1229. struct drm_minor *minor = file_priv->minor;
  1230. struct drm_device *dev = minor->dev;
  1231. struct msm_drm_private *priv = dev->dev_private;
  1232. struct msm_drm_event *node, *temp, *tmp_node;
  1233. u32 count;
  1234. unsigned long flags;
  1235. LIST_HEAD(tmp_head);
  1236. spin_lock_irqsave(&dev->event_lock, flags);
  1237. list_for_each_entry_safe(node, temp, &priv->client_event_list,
  1238. base.link) {
  1239. if (node->base.file_priv != file_priv)
  1240. continue;
  1241. list_del(&node->base.link);
  1242. list_add_tail(&node->base.link, &tmp_head);
  1243. }
  1244. spin_unlock_irqrestore(&dev->event_lock, flags);
  1245. list_for_each_entry_safe(node, temp, &tmp_head,
  1246. base.link) {
  1247. list_del(&node->base.link);
  1248. count = msm_event_client_count(dev, &node->event.info, false);
  1249. list_for_each_entry(tmp_node, &tmp_head, base.link) {
  1250. if (tmp_node->event.base.type ==
  1251. node->event.info.event &&
  1252. tmp_node->event.info.object_id ==
  1253. node->event.info.object_id)
  1254. count++;
  1255. }
  1256. if (!count)
  1257. msm_register_event(dev, &node->event.info, file_priv,
  1258. false);
  1259. kfree(node);
  1260. }
  1261. return drm_release(inode, filp);
  1262. }
  1263. /**
  1264. * msm_ioctl_rmfb2 - remove an FB from the configuration
  1265. * @dev: drm device for the ioctl
  1266. * @data: data pointer for the ioctl
  1267. * @file_priv: drm file for the ioctl call
  1268. *
  1269. * Remove the FB specified by the user.
  1270. *
  1271. * Called by the user via ioctl.
  1272. *
  1273. * Returns:
  1274. * Zero on success, negative errno on failure.
  1275. */
  1276. int msm_ioctl_rmfb2(struct drm_device *dev, void *data,
  1277. struct drm_file *file_priv)
  1278. {
  1279. struct drm_framebuffer *fb = NULL;
  1280. struct drm_framebuffer *fbl = NULL;
  1281. uint32_t *id = data;
  1282. int found = 0;
  1283. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1284. return -EINVAL;
  1285. fb = drm_framebuffer_lookup(dev, file_priv, *id);
  1286. if (!fb)
  1287. return -ENOENT;
  1288. /* drop extra ref from traversing drm_framebuffer_lookup */
  1289. drm_framebuffer_put(fb);
  1290. mutex_lock(&file_priv->fbs_lock);
  1291. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  1292. if (fb == fbl)
  1293. found = 1;
  1294. if (!found) {
  1295. mutex_unlock(&file_priv->fbs_lock);
  1296. return -ENOENT;
  1297. }
  1298. list_del_init(&fb->filp_head);
  1299. mutex_unlock(&file_priv->fbs_lock);
  1300. drm_framebuffer_put(fb);
  1301. return 0;
  1302. }
  1303. EXPORT_SYMBOL(msm_ioctl_rmfb2);
  1304. /**
  1305. * msm_ioctl_power_ctrl - enable/disable power vote on MDSS Hw
  1306. * @dev: drm device for the ioctl
  1307. * @data: data pointer for the ioctl
  1308. * @file_priv: drm file for the ioctl call
  1309. *
  1310. */
  1311. int msm_ioctl_power_ctrl(struct drm_device *dev, void *data,
  1312. struct drm_file *file_priv)
  1313. {
  1314. struct msm_file_private *ctx = file_priv->driver_priv;
  1315. struct msm_drm_private *priv;
  1316. struct drm_msm_power_ctrl *power_ctrl = data;
  1317. bool vote_req = false;
  1318. int old_cnt;
  1319. int rc = 0;
  1320. if (unlikely(!power_ctrl)) {
  1321. DRM_ERROR("invalid ioctl data\n");
  1322. return -EINVAL;
  1323. }
  1324. priv = dev->dev_private;
  1325. mutex_lock(&ctx->power_lock);
  1326. old_cnt = ctx->enable_refcnt;
  1327. if (power_ctrl->enable) {
  1328. if (!ctx->enable_refcnt)
  1329. vote_req = true;
  1330. ctx->enable_refcnt++;
  1331. } else if (ctx->enable_refcnt) {
  1332. ctx->enable_refcnt--;
  1333. if (!ctx->enable_refcnt)
  1334. vote_req = true;
  1335. } else {
  1336. pr_err("ignoring, unbalanced disable\n");
  1337. }
  1338. if (vote_req) {
  1339. if (power_ctrl->enable)
  1340. rc = pm_runtime_get_sync(dev->dev);
  1341. else
  1342. pm_runtime_put_sync(dev->dev);
  1343. if (rc < 0)
  1344. ctx->enable_refcnt = old_cnt;
  1345. else
  1346. rc = 0;
  1347. }
  1348. pr_debug("pid %d enable %d, refcnt %d, vote_req %d\n",
  1349. current->pid, power_ctrl->enable, ctx->enable_refcnt,
  1350. vote_req);
  1351. SDE_EVT32(current->pid, power_ctrl->enable, ctx->enable_refcnt,
  1352. vote_req);
  1353. mutex_unlock(&ctx->power_lock);
  1354. return rc;
  1355. }
  1356. /**
  1357. * msm_ioctl_display_early_wakeup - early wakeup display.
  1358. * @dev: drm device for the ioctl
  1359. * @data: data pointer for the ioctl
  1360. * @file_priv: drm file for the ioctl call
  1361. *
  1362. */
  1363. int msm_ioctl_display_hint_ops(struct drm_device *dev, void *data,
  1364. struct drm_file *file_priv)
  1365. {
  1366. struct drm_msm_display_hint *display_hint = data;
  1367. struct drm_msm_early_wakeup early_wakeup;
  1368. void __user *early_wakeup_usr;
  1369. struct msm_drm_private *priv;
  1370. struct msm_kms *kms;
  1371. priv = dev->dev_private;
  1372. kms = priv->kms;
  1373. if (unlikely(!display_hint)) {
  1374. DRM_ERROR("invalid ioctl data\n");
  1375. return -EINVAL;
  1376. }
  1377. SDE_EVT32(display_hint->hint_flags);
  1378. if (display_hint->hint_flags == DRM_MSM_DISPLAY_EARLY_WAKEUP_HINT) {
  1379. if (!display_hint->data) {
  1380. DRM_ERROR("early_wakeup: wrong parameter\n");
  1381. return -EINVAL;
  1382. }
  1383. early_wakeup_usr =
  1384. (void __user *)((uintptr_t)display_hint->data);
  1385. if (copy_from_user(&early_wakeup, early_wakeup_usr,
  1386. sizeof(early_wakeup))) {
  1387. DRM_ERROR("early_wakeup: copy from user failed\n");
  1388. return -EINVAL;
  1389. }
  1390. SDE_EVT32(early_wakeup.wakeup_hint);
  1391. if (kms && kms->funcs && kms->funcs->display_early_wakeup
  1392. && early_wakeup.wakeup_hint)
  1393. kms->funcs->display_early_wakeup(dev,
  1394. early_wakeup.connector_id);
  1395. }
  1396. return 0;
  1397. }
  1398. static const struct drm_ioctl_desc msm_ioctls[] = {
  1399. DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
  1400. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
  1401. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
  1402. DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_AUTH|DRM_RENDER_ALLOW),
  1403. DRM_IOCTL_DEF_DRV(SDE_WB_CONFIG, sde_wb_config, DRM_UNLOCKED|DRM_AUTH),
  1404. DRM_IOCTL_DEF_DRV(MSM_REGISTER_EVENT, msm_ioctl_register_event,
  1405. DRM_UNLOCKED),
  1406. DRM_IOCTL_DEF_DRV(MSM_DEREGISTER_EVENT, msm_ioctl_deregister_event,
  1407. DRM_UNLOCKED),
  1408. DRM_IOCTL_DEF_DRV(MSM_RMFB2, msm_ioctl_rmfb2, DRM_UNLOCKED),
  1409. DRM_IOCTL_DEF_DRV(MSM_POWER_CTRL, msm_ioctl_power_ctrl,
  1410. DRM_RENDER_ALLOW),
  1411. DRM_IOCTL_DEF_DRV(MSM_DISPLAY_HINT, msm_ioctl_display_hint_ops,
  1412. DRM_UNLOCKED),
  1413. };
  1414. static const struct vm_operations_struct vm_ops = {
  1415. .fault = msm_gem_fault,
  1416. .open = drm_gem_vm_open,
  1417. .close = drm_gem_vm_close,
  1418. };
  1419. static const struct file_operations fops = {
  1420. .owner = THIS_MODULE,
  1421. .open = drm_open,
  1422. .release = msm_release,
  1423. .unlocked_ioctl = drm_ioctl,
  1424. .compat_ioctl = drm_compat_ioctl,
  1425. .poll = drm_poll,
  1426. .read = drm_read,
  1427. .llseek = no_llseek,
  1428. .mmap = msm_gem_mmap,
  1429. };
  1430. static struct drm_driver msm_driver = {
  1431. .driver_features = DRIVER_GEM |
  1432. DRIVER_RENDER |
  1433. DRIVER_ATOMIC |
  1434. DRIVER_MODESET,
  1435. .open = msm_open,
  1436. .postclose = msm_postclose,
  1437. .lastclose = msm_lastclose,
  1438. .irq_handler = msm_irq,
  1439. .irq_preinstall = msm_irq_preinstall,
  1440. .irq_postinstall = msm_irq_postinstall,
  1441. .irq_uninstall = msm_irq_uninstall,
  1442. .enable_vblank = msm_enable_vblank,
  1443. .disable_vblank = msm_disable_vblank,
  1444. .gem_free_object = msm_gem_free_object,
  1445. .gem_vm_ops = &vm_ops,
  1446. .dumb_create = msm_gem_dumb_create,
  1447. .dumb_map_offset = msm_gem_dumb_map_offset,
  1448. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  1449. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  1450. .gem_prime_export = drm_gem_prime_export,
  1451. .gem_prime_import = msm_gem_prime_import,
  1452. .gem_prime_pin = msm_gem_prime_pin,
  1453. .gem_prime_unpin = msm_gem_prime_unpin,
  1454. .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
  1455. .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
  1456. .gem_prime_vmap = msm_gem_prime_vmap,
  1457. .gem_prime_vunmap = msm_gem_prime_vunmap,
  1458. .gem_prime_mmap = msm_gem_prime_mmap,
  1459. .ioctls = msm_ioctls,
  1460. .num_ioctls = ARRAY_SIZE(msm_ioctls),
  1461. .fops = &fops,
  1462. .name = "msm_drm",
  1463. .desc = "MSM Snapdragon DRM",
  1464. .date = "20130625",
  1465. .major = MSM_VERSION_MAJOR,
  1466. .minor = MSM_VERSION_MINOR,
  1467. .patchlevel = MSM_VERSION_PATCHLEVEL,
  1468. };
  1469. #ifdef CONFIG_PM_SLEEP
  1470. static int msm_pm_suspend(struct device *dev)
  1471. {
  1472. struct drm_device *ddev;
  1473. struct msm_drm_private *priv;
  1474. struct msm_kms *kms;
  1475. if (!dev)
  1476. return -EINVAL;
  1477. ddev = dev_get_drvdata(dev);
  1478. if (!ddev || !ddev->dev_private)
  1479. return -EINVAL;
  1480. priv = ddev->dev_private;
  1481. kms = priv->kms;
  1482. if (kms && kms->funcs && kms->funcs->pm_suspend)
  1483. return kms->funcs->pm_suspend(dev);
  1484. /* disable hot-plug polling */
  1485. drm_kms_helper_poll_disable(ddev);
  1486. return 0;
  1487. }
  1488. static int msm_pm_resume(struct device *dev)
  1489. {
  1490. struct drm_device *ddev;
  1491. struct msm_drm_private *priv;
  1492. struct msm_kms *kms;
  1493. if (!dev)
  1494. return -EINVAL;
  1495. ddev = dev_get_drvdata(dev);
  1496. if (!ddev || !ddev->dev_private)
  1497. return -EINVAL;
  1498. priv = ddev->dev_private;
  1499. kms = priv->kms;
  1500. if (kms && kms->funcs && kms->funcs->pm_resume)
  1501. return kms->funcs->pm_resume(dev);
  1502. /* enable hot-plug polling */
  1503. drm_kms_helper_poll_enable(ddev);
  1504. return 0;
  1505. }
  1506. #endif
  1507. #ifdef CONFIG_PM
  1508. static int msm_runtime_suspend(struct device *dev)
  1509. {
  1510. struct drm_device *ddev = dev_get_drvdata(dev);
  1511. struct msm_drm_private *priv = ddev->dev_private;
  1512. DBG("");
  1513. if (priv->mdss)
  1514. msm_mdss_disable(priv->mdss);
  1515. else
  1516. sde_power_resource_enable(&priv->phandle, false);
  1517. return 0;
  1518. }
  1519. static int msm_runtime_resume(struct device *dev)
  1520. {
  1521. struct drm_device *ddev = dev_get_drvdata(dev);
  1522. struct msm_drm_private *priv = ddev->dev_private;
  1523. int ret;
  1524. DBG("");
  1525. if (priv->mdss)
  1526. ret = msm_mdss_enable(priv->mdss);
  1527. else
  1528. ret = sde_power_resource_enable(&priv->phandle, true);
  1529. return ret;
  1530. }
  1531. #endif
  1532. static const struct dev_pm_ops msm_pm_ops = {
  1533. SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
  1534. SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL)
  1535. };
  1536. /*
  1537. * Componentized driver support:
  1538. */
  1539. /*
  1540. * NOTE: duplication of the same code as exynos or imx (or probably any other).
  1541. * so probably some room for some helpers
  1542. */
  1543. static int compare_of(struct device *dev, void *data)
  1544. {
  1545. return dev->of_node == data;
  1546. }
  1547. /*
  1548. * Identify what components need to be added by parsing what remote-endpoints
  1549. * our MDP output ports are connected to. In the case of LVDS on MDP4, there
  1550. * is no external component that we need to add since LVDS is within MDP4
  1551. * itself.
  1552. */
  1553. static int add_components_mdp(struct device *mdp_dev,
  1554. struct component_match **matchptr)
  1555. {
  1556. struct device_node *np = mdp_dev->of_node;
  1557. struct device_node *ep_node;
  1558. struct device *master_dev;
  1559. /*
  1560. * on MDP4 based platforms, the MDP platform device is the component
  1561. * master that adds other display interface components to itself.
  1562. *
  1563. * on MDP5 based platforms, the MDSS platform device is the component
  1564. * master that adds MDP5 and other display interface components to
  1565. * itself.
  1566. */
  1567. if (of_device_is_compatible(np, "qcom,mdp4"))
  1568. master_dev = mdp_dev;
  1569. else
  1570. master_dev = mdp_dev->parent;
  1571. for_each_endpoint_of_node(np, ep_node) {
  1572. struct device_node *intf;
  1573. struct of_endpoint ep;
  1574. int ret;
  1575. ret = of_graph_parse_endpoint(ep_node, &ep);
  1576. if (ret) {
  1577. dev_err(mdp_dev, "unable to parse port endpoint\n");
  1578. of_node_put(ep_node);
  1579. return ret;
  1580. }
  1581. /*
  1582. * The LCDC/LVDS port on MDP4 is a speacial case where the
  1583. * remote-endpoint isn't a component that we need to add
  1584. */
  1585. if (of_device_is_compatible(np, "qcom,mdp4") &&
  1586. ep.port == 0)
  1587. continue;
  1588. /*
  1589. * It's okay if some of the ports don't have a remote endpoint
  1590. * specified. It just means that the port isn't connected to
  1591. * any external interface.
  1592. */
  1593. intf = of_graph_get_remote_port_parent(ep_node);
  1594. if (!intf)
  1595. continue;
  1596. if (of_device_is_available(intf))
  1597. drm_of_component_match_add(master_dev, matchptr,
  1598. compare_of, intf);
  1599. of_node_put(intf);
  1600. }
  1601. return 0;
  1602. }
  1603. static int compare_name_mdp(struct device *dev, void *data)
  1604. {
  1605. return (strnstr(dev_name(dev), "mdp", strlen("mdp")) != NULL);
  1606. }
  1607. static int add_display_components(struct device *dev,
  1608. struct component_match **matchptr)
  1609. {
  1610. struct device *mdp_dev = NULL;
  1611. struct device_node *node;
  1612. int ret;
  1613. if (of_device_is_compatible(dev->of_node, "qcom,sde-kms")) {
  1614. struct device_node *np = dev->of_node;
  1615. unsigned int i;
  1616. for (i = 0; ; i++) {
  1617. node = of_parse_phandle(np, "connectors", i);
  1618. if (!node)
  1619. break;
  1620. component_match_add(dev, matchptr, compare_of, node);
  1621. }
  1622. return 0;
  1623. }
  1624. /*
  1625. * MDP5 based devices don't have a flat hierarchy. There is a top level
  1626. * parent: MDSS, and children: MDP5, DSI, HDMI, eDP etc. Populate the
  1627. * children devices, find the MDP5 node, and then add the interfaces
  1628. * to our components list.
  1629. */
  1630. if (of_device_is_compatible(dev->of_node, "qcom,mdss")) {
  1631. ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
  1632. if (ret) {
  1633. dev_err(dev, "failed to populate children devices\n");
  1634. return ret;
  1635. }
  1636. mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
  1637. if (!mdp_dev) {
  1638. dev_err(dev, "failed to find MDSS MDP node\n");
  1639. of_platform_depopulate(dev);
  1640. return -ENODEV;
  1641. }
  1642. put_device(mdp_dev);
  1643. /* add the MDP component itself */
  1644. component_match_add(dev, matchptr, compare_of,
  1645. mdp_dev->of_node);
  1646. } else {
  1647. /* MDP4 */
  1648. mdp_dev = dev;
  1649. }
  1650. ret = add_components_mdp(mdp_dev, matchptr);
  1651. if (ret)
  1652. of_platform_depopulate(dev);
  1653. return ret;
  1654. }
  1655. struct msm_gem_address_space *
  1656. msm_gem_smmu_address_space_get(struct drm_device *dev,
  1657. unsigned int domain)
  1658. {
  1659. struct msm_drm_private *priv = NULL;
  1660. struct msm_kms *kms;
  1661. const struct msm_kms_funcs *funcs;
  1662. struct msm_gem_address_space *aspace;
  1663. if (!iommu_present(&platform_bus_type))
  1664. return ERR_PTR(-ENODEV);
  1665. if ((!dev) || (!dev->dev_private))
  1666. return ERR_PTR(-EINVAL);
  1667. priv = dev->dev_private;
  1668. kms = priv->kms;
  1669. if (!kms)
  1670. return ERR_PTR(-EINVAL);
  1671. funcs = kms->funcs;
  1672. if ((!funcs) || (!funcs->get_address_space))
  1673. return ERR_PTR(-EINVAL);
  1674. aspace = funcs->get_address_space(priv->kms, domain);
  1675. return aspace ? aspace : ERR_PTR(-EINVAL);
  1676. }
  1677. int msm_get_mixer_count(struct msm_drm_private *priv,
  1678. const struct drm_display_mode *mode,
  1679. const struct msm_resource_caps_info *res, u32 *num_lm)
  1680. {
  1681. struct msm_kms *kms;
  1682. const struct msm_kms_funcs *funcs;
  1683. if (!priv) {
  1684. DRM_ERROR("invalid drm private struct\n");
  1685. return -EINVAL;
  1686. }
  1687. kms = priv->kms;
  1688. if (!kms) {
  1689. DRM_ERROR("invalid msm kms struct\n");
  1690. return -EINVAL;
  1691. }
  1692. funcs = kms->funcs;
  1693. if (!funcs || !funcs->get_mixer_count) {
  1694. DRM_ERROR("invalid function pointers\n");
  1695. return -EINVAL;
  1696. }
  1697. return funcs->get_mixer_count(priv->kms, mode, res, num_lm);
  1698. }
  1699. int msm_get_dsc_count(struct msm_drm_private *priv,
  1700. u32 hdisplay, u32 *num_dsc)
  1701. {
  1702. struct msm_kms *kms;
  1703. const struct msm_kms_funcs *funcs;
  1704. if (!priv) {
  1705. DRM_ERROR("invalid drm private struct\n");
  1706. return -EINVAL;
  1707. }
  1708. kms = priv->kms;
  1709. if (!kms) {
  1710. DRM_ERROR("invalid msm kms struct\n");
  1711. return -EINVAL;
  1712. }
  1713. funcs = kms->funcs;
  1714. if (!funcs || !funcs->get_dsc_count) {
  1715. DRM_ERROR("invalid function pointers\n");
  1716. return -EINVAL;
  1717. }
  1718. return funcs->get_dsc_count(priv->kms, hdisplay, num_dsc);
  1719. }
  1720. static int msm_drm_bind(struct device *dev)
  1721. {
  1722. return msm_drm_component_init(dev);
  1723. }
  1724. static void msm_drm_unbind(struct device *dev)
  1725. {
  1726. msm_drm_uninit(dev);
  1727. }
  1728. static const struct component_master_ops msm_drm_ops = {
  1729. .bind = msm_drm_bind,
  1730. .unbind = msm_drm_unbind,
  1731. };
  1732. static int msm_drm_component_dependency_check(struct device *dev)
  1733. {
  1734. struct device_node *node;
  1735. struct device_node *np = dev->of_node;
  1736. unsigned int i;
  1737. if (!of_device_is_compatible(dev->of_node, "qcom,sde-kms"))
  1738. return 0;
  1739. for (i = 0; ; i++) {
  1740. node = of_parse_phandle(np, "connectors", i);
  1741. if (!node)
  1742. break;
  1743. if (of_node_name_eq(node,"qcom,sde_rscc") &&
  1744. of_device_is_available(node) &&
  1745. of_node_check_flag(node, OF_POPULATED)) {
  1746. struct platform_device *pdev =
  1747. of_find_device_by_node(node);
  1748. if (!platform_get_drvdata(pdev)) {
  1749. dev_err(dev,
  1750. "qcom,sde_rscc not probed yet\n");
  1751. return -EPROBE_DEFER;
  1752. } else {
  1753. return 0;
  1754. }
  1755. }
  1756. }
  1757. return 0;
  1758. }
  1759. /*
  1760. * Platform driver:
  1761. */
  1762. static int msm_pdev_probe(struct platform_device *pdev)
  1763. {
  1764. int ret;
  1765. struct component_match *match = NULL;
  1766. ret = msm_drm_component_dependency_check(&pdev->dev);
  1767. if (ret)
  1768. return ret;
  1769. ret = msm_drm_device_init(pdev, &msm_driver);
  1770. if (ret)
  1771. return ret;
  1772. ret = add_display_components(&pdev->dev, &match);
  1773. if (ret)
  1774. return ret;
  1775. if (!match)
  1776. return -ENODEV;
  1777. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  1778. return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
  1779. }
  1780. static int msm_pdev_remove(struct platform_device *pdev)
  1781. {
  1782. component_master_del(&pdev->dev, &msm_drm_ops);
  1783. of_platform_depopulate(&pdev->dev);
  1784. return 0;
  1785. }
  1786. static void msm_pdev_shutdown(struct platform_device *pdev)
  1787. {
  1788. struct drm_device *ddev = platform_get_drvdata(pdev);
  1789. struct msm_drm_private *priv = NULL;
  1790. if (!ddev) {
  1791. DRM_ERROR("invalid drm device node\n");
  1792. return;
  1793. }
  1794. priv = ddev->dev_private;
  1795. if (!priv) {
  1796. DRM_ERROR("invalid msm drm private node\n");
  1797. return;
  1798. }
  1799. msm_lastclose(ddev);
  1800. /* set this after lastclose to allow kickoff from lastclose */
  1801. priv->shutdown_in_progress = true;
  1802. }
  1803. static const struct of_device_id dt_match[] = {
  1804. { .compatible = "qcom,mdp4", .data = (void *)KMS_MDP4 },
  1805. { .compatible = "qcom,mdss", .data = (void *)KMS_MDP5 },
  1806. { .compatible = "qcom,sde-kms", .data = (void *)KMS_SDE },
  1807. {},
  1808. };
  1809. MODULE_DEVICE_TABLE(of, dt_match);
  1810. static struct platform_driver msm_platform_driver = {
  1811. .probe = msm_pdev_probe,
  1812. .remove = msm_pdev_remove,
  1813. .shutdown = msm_pdev_shutdown,
  1814. .driver = {
  1815. .name = "msm_drm",
  1816. .of_match_table = dt_match,
  1817. .pm = &msm_pm_ops,
  1818. .suppress_bind_attrs = true,
  1819. },
  1820. };
  1821. static int __init msm_drm_register(void)
  1822. {
  1823. if (!modeset)
  1824. return -EINVAL;
  1825. DBG("init");
  1826. sde_rsc_rpmh_register();
  1827. sde_rsc_register();
  1828. dsi_display_register();
  1829. msm_hdcp_register();
  1830. dp_display_register();
  1831. msm_smmu_driver_init();
  1832. sde_rotator_register();
  1833. sde_rotator_smmu_driver_register();
  1834. msm_dsi_register();
  1835. msm_edp_register();
  1836. msm_hdmi_register();
  1837. sde_wb_register();
  1838. return platform_driver_register(&msm_platform_driver);
  1839. }
  1840. static void __exit msm_drm_unregister(void)
  1841. {
  1842. DBG("fini");
  1843. platform_driver_unregister(&msm_platform_driver);
  1844. sde_wb_unregister();
  1845. msm_hdmi_unregister();
  1846. msm_edp_unregister();
  1847. msm_dsi_unregister();
  1848. sde_rotator_smmu_driver_unregister();
  1849. sde_rotator_unregister();
  1850. msm_smmu_driver_cleanup();
  1851. msm_hdcp_unregister();
  1852. dp_display_unregister();
  1853. dsi_display_unregister();
  1854. sde_rsc_unregister();
  1855. }
  1856. module_init(msm_drm_register);
  1857. module_exit(msm_drm_unregister);
  1858. MODULE_AUTHOR("Rob Clark <[email protected]");
  1859. MODULE_DESCRIPTION("MSM DRM Driver");
  1860. MODULE_LICENSE("GPL");