msm_drv.c 57 KB

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