msm_drv.c 55 KB

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