msm_drv.c 52 KB

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