msm_drv.c 54 KB

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