msm_drv.c 55 KB

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