msm_drv.c 55 KB

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