msm_drv.c 54 KB

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