msm_drv.c 54 KB

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