msm_drv.c 55 KB

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