msm_drv.c 54 KB

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