msm_drv.c 60 KB

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