msm_drv.c 54 KB

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