msm_drv.c 59 KB

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