msm_drv.c 55 KB

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