msm_drv.c 55 KB

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