msm_drv.c 54 KB

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