msm_drv.c 53 KB

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