msm_drv.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <drm/drm_of.h>
  18. #include "msm_drv.h"
  19. #include "msm_debugfs.h"
  20. #include "msm_fence.h"
  21. #include "msm_gpu.h"
  22. #include "msm_kms.h"
  23. /*
  24. * MSM driver version:
  25. * - 1.0.0 - initial interface
  26. * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
  27. * - 1.2.0 - adds explicit fence support for submit ioctl
  28. */
  29. #define MSM_VERSION_MAJOR 1
  30. #define MSM_VERSION_MINOR 2
  31. #define MSM_VERSION_PATCHLEVEL 0
  32. static void msm_fb_output_poll_changed(struct drm_device *dev)
  33. {
  34. struct msm_drm_private *priv = dev->dev_private;
  35. if (priv->fbdev)
  36. drm_fb_helper_hotplug_event(priv->fbdev);
  37. }
  38. static const struct drm_mode_config_funcs mode_config_funcs = {
  39. .fb_create = msm_framebuffer_create,
  40. .output_poll_changed = msm_fb_output_poll_changed,
  41. .atomic_check = msm_atomic_check,
  42. .atomic_commit = msm_atomic_commit,
  43. .atomic_state_alloc = msm_atomic_state_alloc,
  44. .atomic_state_clear = msm_atomic_state_clear,
  45. .atomic_state_free = msm_atomic_state_free,
  46. };
  47. #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
  48. static bool reglog = false;
  49. MODULE_PARM_DESC(reglog, "Enable register read/write logging");
  50. module_param(reglog, bool, 0600);
  51. #else
  52. #define reglog 0
  53. #endif
  54. #ifdef CONFIG_DRM_FBDEV_EMULATION
  55. static bool fbdev = true;
  56. MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
  57. module_param(fbdev, bool, 0600);
  58. #endif
  59. static char *vram = "16m";
  60. MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
  61. module_param(vram, charp, 0);
  62. bool dumpstate = false;
  63. MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
  64. module_param(dumpstate, bool, 0600);
  65. /*
  66. * Util/helpers:
  67. */
  68. struct clk *msm_clk_get(struct platform_device *pdev, const char *name)
  69. {
  70. struct clk *clk;
  71. char name2[32];
  72. clk = devm_clk_get(&pdev->dev, name);
  73. if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
  74. return clk;
  75. snprintf(name2, sizeof(name2), "%s_clk", name);
  76. clk = devm_clk_get(&pdev->dev, name2);
  77. if (!IS_ERR(clk))
  78. dev_warn(&pdev->dev, "Using legacy clk name binding. Use "
  79. "\"%s\" instead of \"%s\"\n", name, name2);
  80. return clk;
  81. }
  82. void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
  83. const char *dbgname)
  84. {
  85. struct resource *res;
  86. unsigned long size;
  87. void __iomem *ptr;
  88. if (name)
  89. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  90. else
  91. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  92. if (!res) {
  93. dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
  94. return ERR_PTR(-EINVAL);
  95. }
  96. size = resource_size(res);
  97. ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
  98. if (!ptr) {
  99. dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
  100. return ERR_PTR(-ENOMEM);
  101. }
  102. if (reglog)
  103. printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
  104. return ptr;
  105. }
  106. void msm_writel(u32 data, void __iomem *addr)
  107. {
  108. if (reglog)
  109. printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
  110. writel(data, addr);
  111. }
  112. u32 msm_readl(const void __iomem *addr)
  113. {
  114. u32 val = readl(addr);
  115. if (reglog)
  116. pr_err("IO:R %p %08x\n", addr, val);
  117. return val;
  118. }
  119. struct vblank_event {
  120. struct list_head node;
  121. int crtc_id;
  122. bool enable;
  123. };
  124. static void vblank_ctrl_worker(struct work_struct *work)
  125. {
  126. struct msm_vblank_ctrl *vbl_ctrl = container_of(work,
  127. struct msm_vblank_ctrl, work);
  128. struct msm_drm_private *priv = container_of(vbl_ctrl,
  129. struct msm_drm_private, vblank_ctrl);
  130. struct msm_kms *kms = priv->kms;
  131. struct vblank_event *vbl_ev, *tmp;
  132. unsigned long flags;
  133. spin_lock_irqsave(&vbl_ctrl->lock, flags);
  134. list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
  135. list_del(&vbl_ev->node);
  136. spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
  137. if (vbl_ev->enable)
  138. kms->funcs->enable_vblank(kms,
  139. priv->crtcs[vbl_ev->crtc_id]);
  140. else
  141. kms->funcs->disable_vblank(kms,
  142. priv->crtcs[vbl_ev->crtc_id]);
  143. kfree(vbl_ev);
  144. spin_lock_irqsave(&vbl_ctrl->lock, flags);
  145. }
  146. spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
  147. }
  148. static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
  149. int crtc_id, bool enable)
  150. {
  151. struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
  152. struct vblank_event *vbl_ev;
  153. unsigned long flags;
  154. vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC);
  155. if (!vbl_ev)
  156. return -ENOMEM;
  157. vbl_ev->crtc_id = crtc_id;
  158. vbl_ev->enable = enable;
  159. spin_lock_irqsave(&vbl_ctrl->lock, flags);
  160. list_add_tail(&vbl_ev->node, &vbl_ctrl->event_list);
  161. spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
  162. queue_work(priv->wq, &vbl_ctrl->work);
  163. return 0;
  164. }
  165. static int msm_drm_uninit(struct device *dev)
  166. {
  167. struct platform_device *pdev = to_platform_device(dev);
  168. struct drm_device *ddev = platform_get_drvdata(pdev);
  169. struct msm_drm_private *priv = ddev->dev_private;
  170. struct msm_kms *kms = priv->kms;
  171. struct msm_gpu *gpu = priv->gpu;
  172. struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
  173. struct vblank_event *vbl_ev, *tmp;
  174. /* We must cancel and cleanup any pending vblank enable/disable
  175. * work before drm_irq_uninstall() to avoid work re-enabling an
  176. * irq after uninstall has disabled it.
  177. */
  178. cancel_work_sync(&vbl_ctrl->work);
  179. list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
  180. list_del(&vbl_ev->node);
  181. kfree(vbl_ev);
  182. }
  183. msm_gem_shrinker_cleanup(ddev);
  184. drm_kms_helper_poll_fini(ddev);
  185. drm_dev_unregister(ddev);
  186. msm_perf_debugfs_cleanup(priv);
  187. msm_rd_debugfs_cleanup(priv);
  188. #ifdef CONFIG_DRM_FBDEV_EMULATION
  189. if (fbdev && priv->fbdev)
  190. msm_fbdev_free(ddev);
  191. #endif
  192. drm_mode_config_cleanup(ddev);
  193. pm_runtime_get_sync(dev);
  194. drm_irq_uninstall(ddev);
  195. pm_runtime_put_sync(dev);
  196. flush_workqueue(priv->wq);
  197. destroy_workqueue(priv->wq);
  198. flush_workqueue(priv->atomic_wq);
  199. destroy_workqueue(priv->atomic_wq);
  200. if (kms && kms->funcs)
  201. kms->funcs->destroy(kms);
  202. if (gpu) {
  203. mutex_lock(&ddev->struct_mutex);
  204. // XXX what do we do here?
  205. //pm_runtime_enable(&pdev->dev);
  206. gpu->funcs->pm_suspend(gpu);
  207. mutex_unlock(&ddev->struct_mutex);
  208. gpu->funcs->destroy(gpu);
  209. }
  210. if (priv->vram.paddr) {
  211. unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
  212. drm_mm_takedown(&priv->vram.mm);
  213. dma_free_attrs(dev, priv->vram.size, NULL,
  214. priv->vram.paddr, attrs);
  215. }
  216. component_unbind_all(dev, ddev);
  217. msm_mdss_destroy(ddev);
  218. ddev->dev_private = NULL;
  219. drm_dev_unref(ddev);
  220. kfree(priv);
  221. return 0;
  222. }
  223. static int get_mdp_ver(struct platform_device *pdev)
  224. {
  225. struct device *dev = &pdev->dev;
  226. return (int) (unsigned long) of_device_get_match_data(dev);
  227. }
  228. #include <linux/of_address.h>
  229. static int msm_init_vram(struct drm_device *dev)
  230. {
  231. struct msm_drm_private *priv = dev->dev_private;
  232. struct device_node *node;
  233. unsigned long size = 0;
  234. int ret = 0;
  235. /* In the device-tree world, we could have a 'memory-region'
  236. * phandle, which gives us a link to our "vram". Allocating
  237. * is all nicely abstracted behind the dma api, but we need
  238. * to know the entire size to allocate it all in one go. There
  239. * are two cases:
  240. * 1) device with no IOMMU, in which case we need exclusive
  241. * access to a VRAM carveout big enough for all gpu
  242. * buffers
  243. * 2) device with IOMMU, but where the bootloader puts up
  244. * a splash screen. In this case, the VRAM carveout
  245. * need only be large enough for fbdev fb. But we need
  246. * exclusive access to the buffer to avoid the kernel
  247. * using those pages for other purposes (which appears
  248. * as corruption on screen before we have a chance to
  249. * load and do initial modeset)
  250. */
  251. node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
  252. if (node) {
  253. struct resource r;
  254. ret = of_address_to_resource(node, 0, &r);
  255. of_node_put(node);
  256. if (ret)
  257. return ret;
  258. size = r.end - r.start;
  259. DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
  260. /* if we have no IOMMU, then we need to use carveout allocator.
  261. * Grab the entire CMA chunk carved out in early startup in
  262. * mach-msm:
  263. */
  264. } else if (!iommu_present(&platform_bus_type)) {
  265. DRM_INFO("using %s VRAM carveout\n", vram);
  266. size = memparse(vram, NULL);
  267. }
  268. if (size) {
  269. unsigned long attrs = 0;
  270. void *p;
  271. priv->vram.size = size;
  272. drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
  273. spin_lock_init(&priv->vram.lock);
  274. attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
  275. attrs |= DMA_ATTR_WRITE_COMBINE;
  276. /* note that for no-kernel-mapping, the vaddr returned
  277. * is bogus, but non-null if allocation succeeded:
  278. */
  279. p = dma_alloc_attrs(dev->dev, size,
  280. &priv->vram.paddr, GFP_KERNEL, attrs);
  281. if (!p) {
  282. dev_err(dev->dev, "failed to allocate VRAM\n");
  283. priv->vram.paddr = 0;
  284. return -ENOMEM;
  285. }
  286. dev_info(dev->dev, "VRAM: %08x->%08x\n",
  287. (uint32_t)priv->vram.paddr,
  288. (uint32_t)(priv->vram.paddr + size));
  289. }
  290. return ret;
  291. }
  292. static int msm_drm_init(struct device *dev, struct drm_driver *drv)
  293. {
  294. struct platform_device *pdev = to_platform_device(dev);
  295. struct drm_device *ddev;
  296. struct msm_drm_private *priv;
  297. struct msm_kms *kms;
  298. int ret;
  299. ddev = drm_dev_alloc(drv, dev);
  300. if (IS_ERR(ddev)) {
  301. dev_err(dev, "failed to allocate drm_device\n");
  302. return PTR_ERR(ddev);
  303. }
  304. platform_set_drvdata(pdev, ddev);
  305. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  306. if (!priv) {
  307. drm_dev_unref(ddev);
  308. return -ENOMEM;
  309. }
  310. ddev->dev_private = priv;
  311. priv->dev = ddev;
  312. ret = msm_mdss_init(ddev);
  313. if (ret) {
  314. kfree(priv);
  315. drm_dev_unref(ddev);
  316. return ret;
  317. }
  318. priv->wq = alloc_ordered_workqueue("msm", 0);
  319. priv->atomic_wq = alloc_ordered_workqueue("msm:atomic", 0);
  320. init_waitqueue_head(&priv->pending_crtcs_event);
  321. INIT_LIST_HEAD(&priv->inactive_list);
  322. INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
  323. INIT_WORK(&priv->vblank_ctrl.work, vblank_ctrl_worker);
  324. spin_lock_init(&priv->vblank_ctrl.lock);
  325. drm_mode_config_init(ddev);
  326. /* Bind all our sub-components: */
  327. ret = component_bind_all(dev, ddev);
  328. if (ret) {
  329. msm_mdss_destroy(ddev);
  330. kfree(priv);
  331. drm_dev_unref(ddev);
  332. return ret;
  333. }
  334. ret = msm_init_vram(ddev);
  335. if (ret)
  336. goto fail;
  337. msm_gem_shrinker_init(ddev);
  338. switch (get_mdp_ver(pdev)) {
  339. case 4:
  340. kms = mdp4_kms_init(ddev);
  341. priv->kms = kms;
  342. break;
  343. case 5:
  344. kms = mdp5_kms_init(ddev);
  345. break;
  346. default:
  347. kms = ERR_PTR(-ENODEV);
  348. break;
  349. }
  350. if (IS_ERR(kms)) {
  351. /*
  352. * NOTE: once we have GPU support, having no kms should not
  353. * be considered fatal.. ideally we would still support gpu
  354. * and (for example) use dmabuf/prime to share buffers with
  355. * imx drm driver on iMX5
  356. */
  357. dev_err(dev, "failed to load kms\n");
  358. ret = PTR_ERR(kms);
  359. goto fail;
  360. }
  361. if (kms) {
  362. ret = kms->funcs->hw_init(kms);
  363. if (ret) {
  364. dev_err(dev, "kms hw init failed: %d\n", ret);
  365. goto fail;
  366. }
  367. }
  368. ddev->mode_config.funcs = &mode_config_funcs;
  369. ret = drm_vblank_init(ddev, priv->num_crtcs);
  370. if (ret < 0) {
  371. dev_err(dev, "failed to initialize vblank\n");
  372. goto fail;
  373. }
  374. if (kms) {
  375. pm_runtime_get_sync(dev);
  376. ret = drm_irq_install(ddev, kms->irq);
  377. pm_runtime_put_sync(dev);
  378. if (ret < 0) {
  379. dev_err(dev, "failed to install IRQ handler\n");
  380. goto fail;
  381. }
  382. }
  383. ret = drm_dev_register(ddev, 0);
  384. if (ret)
  385. goto fail;
  386. drm_mode_config_reset(ddev);
  387. #ifdef CONFIG_DRM_FBDEV_EMULATION
  388. if (fbdev)
  389. priv->fbdev = msm_fbdev_init(ddev);
  390. #endif
  391. ret = msm_debugfs_late_init(ddev);
  392. if (ret)
  393. goto fail;
  394. drm_kms_helper_poll_init(ddev);
  395. return 0;
  396. fail:
  397. msm_drm_uninit(dev);
  398. return ret;
  399. }
  400. /*
  401. * DRM operations:
  402. */
  403. static void load_gpu(struct drm_device *dev)
  404. {
  405. static DEFINE_MUTEX(init_lock);
  406. struct msm_drm_private *priv = dev->dev_private;
  407. mutex_lock(&init_lock);
  408. if (!priv->gpu)
  409. priv->gpu = adreno_load_gpu(dev);
  410. mutex_unlock(&init_lock);
  411. }
  412. static int msm_open(struct drm_device *dev, struct drm_file *file)
  413. {
  414. struct msm_file_private *ctx;
  415. /* For now, load gpu on open.. to avoid the requirement of having
  416. * firmware in the initrd.
  417. */
  418. load_gpu(dev);
  419. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  420. if (!ctx)
  421. return -ENOMEM;
  422. file->driver_priv = ctx;
  423. return 0;
  424. }
  425. static void msm_postclose(struct drm_device *dev, struct drm_file *file)
  426. {
  427. struct msm_drm_private *priv = dev->dev_private;
  428. struct msm_file_private *ctx = file->driver_priv;
  429. mutex_lock(&dev->struct_mutex);
  430. if (ctx == priv->lastctx)
  431. priv->lastctx = NULL;
  432. mutex_unlock(&dev->struct_mutex);
  433. kfree(ctx);
  434. }
  435. static void msm_lastclose(struct drm_device *dev)
  436. {
  437. struct msm_drm_private *priv = dev->dev_private;
  438. if (priv->fbdev)
  439. drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  440. }
  441. static irqreturn_t msm_irq(int irq, void *arg)
  442. {
  443. struct drm_device *dev = arg;
  444. struct msm_drm_private *priv = dev->dev_private;
  445. struct msm_kms *kms = priv->kms;
  446. BUG_ON(!kms);
  447. return kms->funcs->irq(kms);
  448. }
  449. static void msm_irq_preinstall(struct drm_device *dev)
  450. {
  451. struct msm_drm_private *priv = dev->dev_private;
  452. struct msm_kms *kms = priv->kms;
  453. BUG_ON(!kms);
  454. kms->funcs->irq_preinstall(kms);
  455. }
  456. static int msm_irq_postinstall(struct drm_device *dev)
  457. {
  458. struct msm_drm_private *priv = dev->dev_private;
  459. struct msm_kms *kms = priv->kms;
  460. BUG_ON(!kms);
  461. return kms->funcs->irq_postinstall(kms);
  462. }
  463. static void msm_irq_uninstall(struct drm_device *dev)
  464. {
  465. struct msm_drm_private *priv = dev->dev_private;
  466. struct msm_kms *kms = priv->kms;
  467. BUG_ON(!kms);
  468. kms->funcs->irq_uninstall(kms);
  469. }
  470. static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
  471. {
  472. struct msm_drm_private *priv = dev->dev_private;
  473. struct msm_kms *kms = priv->kms;
  474. if (!kms)
  475. return -ENXIO;
  476. DBG("dev=%p, crtc=%u", dev, pipe);
  477. return vblank_ctrl_queue_work(priv, pipe, true);
  478. }
  479. static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
  480. {
  481. struct msm_drm_private *priv = dev->dev_private;
  482. struct msm_kms *kms = priv->kms;
  483. if (!kms)
  484. return;
  485. DBG("dev=%p, crtc=%u", dev, pipe);
  486. vblank_ctrl_queue_work(priv, pipe, false);
  487. }
  488. /*
  489. * DRM ioctls:
  490. */
  491. static int msm_ioctl_get_param(struct drm_device *dev, void *data,
  492. struct drm_file *file)
  493. {
  494. struct msm_drm_private *priv = dev->dev_private;
  495. struct drm_msm_param *args = data;
  496. struct msm_gpu *gpu;
  497. /* for now, we just have 3d pipe.. eventually this would need to
  498. * be more clever to dispatch to appropriate gpu module:
  499. */
  500. if (args->pipe != MSM_PIPE_3D0)
  501. return -EINVAL;
  502. gpu = priv->gpu;
  503. if (!gpu)
  504. return -ENXIO;
  505. return gpu->funcs->get_param(gpu, args->param, &args->value);
  506. }
  507. static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
  508. struct drm_file *file)
  509. {
  510. struct drm_msm_gem_new *args = data;
  511. if (args->flags & ~MSM_BO_FLAGS) {
  512. DRM_ERROR("invalid flags: %08x\n", args->flags);
  513. return -EINVAL;
  514. }
  515. return msm_gem_new_handle(dev, file, args->size,
  516. args->flags, &args->handle);
  517. }
  518. static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
  519. {
  520. return ktime_set(timeout.tv_sec, timeout.tv_nsec);
  521. }
  522. static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  523. struct drm_file *file)
  524. {
  525. struct drm_msm_gem_cpu_prep *args = data;
  526. struct drm_gem_object *obj;
  527. ktime_t timeout = to_ktime(args->timeout);
  528. int ret;
  529. if (args->op & ~MSM_PREP_FLAGS) {
  530. DRM_ERROR("invalid op: %08x\n", args->op);
  531. return -EINVAL;
  532. }
  533. obj = drm_gem_object_lookup(file, args->handle);
  534. if (!obj)
  535. return -ENOENT;
  536. ret = msm_gem_cpu_prep(obj, args->op, &timeout);
  537. drm_gem_object_unreference_unlocked(obj);
  538. return ret;
  539. }
  540. static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  541. struct drm_file *file)
  542. {
  543. struct drm_msm_gem_cpu_fini *args = data;
  544. struct drm_gem_object *obj;
  545. int ret;
  546. obj = drm_gem_object_lookup(file, args->handle);
  547. if (!obj)
  548. return -ENOENT;
  549. ret = msm_gem_cpu_fini(obj);
  550. drm_gem_object_unreference_unlocked(obj);
  551. return ret;
  552. }
  553. static int msm_ioctl_gem_info_iova(struct drm_device *dev,
  554. struct drm_gem_object *obj, uint64_t *iova)
  555. {
  556. struct msm_drm_private *priv = dev->dev_private;
  557. if (!priv->gpu)
  558. return -EINVAL;
  559. return msm_gem_get_iova(obj, priv->gpu->aspace, iova);
  560. }
  561. static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
  562. struct drm_file *file)
  563. {
  564. struct drm_msm_gem_info *args = data;
  565. struct drm_gem_object *obj;
  566. int ret = 0;
  567. if (args->flags & ~MSM_INFO_FLAGS)
  568. return -EINVAL;
  569. obj = drm_gem_object_lookup(file, args->handle);
  570. if (!obj)
  571. return -ENOENT;
  572. if (args->flags & MSM_INFO_IOVA) {
  573. uint64_t iova;
  574. ret = msm_ioctl_gem_info_iova(dev, obj, &iova);
  575. if (!ret)
  576. args->offset = iova;
  577. } else {
  578. args->offset = msm_gem_mmap_offset(obj);
  579. }
  580. drm_gem_object_unreference_unlocked(obj);
  581. return ret;
  582. }
  583. static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
  584. struct drm_file *file)
  585. {
  586. struct msm_drm_private *priv = dev->dev_private;
  587. struct drm_msm_wait_fence *args = data;
  588. ktime_t timeout = to_ktime(args->timeout);
  589. if (args->pad) {
  590. DRM_ERROR("invalid pad: %08x\n", args->pad);
  591. return -EINVAL;
  592. }
  593. if (!priv->gpu)
  594. return 0;
  595. return msm_wait_fence(priv->gpu->fctx, args->fence, &timeout, true);
  596. }
  597. static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
  598. struct drm_file *file)
  599. {
  600. struct drm_msm_gem_madvise *args = data;
  601. struct drm_gem_object *obj;
  602. int ret;
  603. switch (args->madv) {
  604. case MSM_MADV_DONTNEED:
  605. case MSM_MADV_WILLNEED:
  606. break;
  607. default:
  608. return -EINVAL;
  609. }
  610. ret = mutex_lock_interruptible(&dev->struct_mutex);
  611. if (ret)
  612. return ret;
  613. obj = drm_gem_object_lookup(file, args->handle);
  614. if (!obj) {
  615. ret = -ENOENT;
  616. goto unlock;
  617. }
  618. ret = msm_gem_madvise(obj, args->madv);
  619. if (ret >= 0) {
  620. args->retained = ret;
  621. ret = 0;
  622. }
  623. drm_gem_object_unreference(obj);
  624. unlock:
  625. mutex_unlock(&dev->struct_mutex);
  626. return ret;
  627. }
  628. static const struct drm_ioctl_desc msm_ioctls[] = {
  629. DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW),
  630. DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
  631. DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
  632. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
  633. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
  634. DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
  635. DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_AUTH|DRM_RENDER_ALLOW),
  636. DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_AUTH|DRM_RENDER_ALLOW),
  637. };
  638. static const struct vm_operations_struct vm_ops = {
  639. .fault = msm_gem_fault,
  640. .open = drm_gem_vm_open,
  641. .close = drm_gem_vm_close,
  642. };
  643. static const struct file_operations fops = {
  644. .owner = THIS_MODULE,
  645. .open = drm_open,
  646. .release = drm_release,
  647. .unlocked_ioctl = drm_ioctl,
  648. .compat_ioctl = drm_compat_ioctl,
  649. .poll = drm_poll,
  650. .read = drm_read,
  651. .llseek = no_llseek,
  652. .mmap = msm_gem_mmap,
  653. };
  654. static struct drm_driver msm_driver = {
  655. .driver_features = DRIVER_HAVE_IRQ |
  656. DRIVER_GEM |
  657. DRIVER_PRIME |
  658. DRIVER_RENDER |
  659. DRIVER_ATOMIC |
  660. DRIVER_MODESET,
  661. .open = msm_open,
  662. .postclose = msm_postclose,
  663. .lastclose = msm_lastclose,
  664. .irq_handler = msm_irq,
  665. .irq_preinstall = msm_irq_preinstall,
  666. .irq_postinstall = msm_irq_postinstall,
  667. .irq_uninstall = msm_irq_uninstall,
  668. .enable_vblank = msm_enable_vblank,
  669. .disable_vblank = msm_disable_vblank,
  670. .gem_free_object = msm_gem_free_object,
  671. .gem_vm_ops = &vm_ops,
  672. .dumb_create = msm_gem_dumb_create,
  673. .dumb_map_offset = msm_gem_dumb_map_offset,
  674. .dumb_destroy = drm_gem_dumb_destroy,
  675. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  676. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  677. .gem_prime_export = drm_gem_prime_export,
  678. .gem_prime_import = drm_gem_prime_import,
  679. .gem_prime_res_obj = msm_gem_prime_res_obj,
  680. .gem_prime_pin = msm_gem_prime_pin,
  681. .gem_prime_unpin = msm_gem_prime_unpin,
  682. .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
  683. .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
  684. .gem_prime_vmap = msm_gem_prime_vmap,
  685. .gem_prime_vunmap = msm_gem_prime_vunmap,
  686. .gem_prime_mmap = msm_gem_prime_mmap,
  687. #ifdef CONFIG_DEBUG_FS
  688. .debugfs_init = msm_debugfs_init,
  689. #endif
  690. .ioctls = msm_ioctls,
  691. .num_ioctls = ARRAY_SIZE(msm_ioctls),
  692. .fops = &fops,
  693. .name = "msm",
  694. .desc = "MSM Snapdragon DRM",
  695. .date = "20130625",
  696. .major = MSM_VERSION_MAJOR,
  697. .minor = MSM_VERSION_MINOR,
  698. .patchlevel = MSM_VERSION_PATCHLEVEL,
  699. };
  700. #ifdef CONFIG_PM_SLEEP
  701. static int msm_pm_suspend(struct device *dev)
  702. {
  703. struct drm_device *ddev = dev_get_drvdata(dev);
  704. drm_kms_helper_poll_disable(ddev);
  705. return 0;
  706. }
  707. static int msm_pm_resume(struct device *dev)
  708. {
  709. struct drm_device *ddev = dev_get_drvdata(dev);
  710. drm_kms_helper_poll_enable(ddev);
  711. return 0;
  712. }
  713. #endif
  714. #ifdef CONFIG_PM
  715. static int msm_runtime_suspend(struct device *dev)
  716. {
  717. struct drm_device *ddev = dev_get_drvdata(dev);
  718. struct msm_drm_private *priv = ddev->dev_private;
  719. DBG("");
  720. if (priv->mdss)
  721. return msm_mdss_disable(priv->mdss);
  722. return 0;
  723. }
  724. static int msm_runtime_resume(struct device *dev)
  725. {
  726. struct drm_device *ddev = dev_get_drvdata(dev);
  727. struct msm_drm_private *priv = ddev->dev_private;
  728. DBG("");
  729. if (priv->mdss)
  730. return msm_mdss_enable(priv->mdss);
  731. return 0;
  732. }
  733. #endif
  734. static const struct dev_pm_ops msm_pm_ops = {
  735. SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
  736. SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL)
  737. };
  738. /*
  739. * Componentized driver support:
  740. */
  741. /*
  742. * NOTE: duplication of the same code as exynos or imx (or probably any other).
  743. * so probably some room for some helpers
  744. */
  745. static int compare_of(struct device *dev, void *data)
  746. {
  747. return dev->of_node == data;
  748. }
  749. /*
  750. * Identify what components need to be added by parsing what remote-endpoints
  751. * our MDP output ports are connected to. In the case of LVDS on MDP4, there
  752. * is no external component that we need to add since LVDS is within MDP4
  753. * itself.
  754. */
  755. static int add_components_mdp(struct device *mdp_dev,
  756. struct component_match **matchptr)
  757. {
  758. struct device_node *np = mdp_dev->of_node;
  759. struct device_node *ep_node;
  760. struct device *master_dev;
  761. /*
  762. * on MDP4 based platforms, the MDP platform device is the component
  763. * master that adds other display interface components to itself.
  764. *
  765. * on MDP5 based platforms, the MDSS platform device is the component
  766. * master that adds MDP5 and other display interface components to
  767. * itself.
  768. */
  769. if (of_device_is_compatible(np, "qcom,mdp4"))
  770. master_dev = mdp_dev;
  771. else
  772. master_dev = mdp_dev->parent;
  773. for_each_endpoint_of_node(np, ep_node) {
  774. struct device_node *intf;
  775. struct of_endpoint ep;
  776. int ret;
  777. ret = of_graph_parse_endpoint(ep_node, &ep);
  778. if (ret) {
  779. dev_err(mdp_dev, "unable to parse port endpoint\n");
  780. of_node_put(ep_node);
  781. return ret;
  782. }
  783. /*
  784. * The LCDC/LVDS port on MDP4 is a speacial case where the
  785. * remote-endpoint isn't a component that we need to add
  786. */
  787. if (of_device_is_compatible(np, "qcom,mdp4") &&
  788. ep.port == 0)
  789. continue;
  790. /*
  791. * It's okay if some of the ports don't have a remote endpoint
  792. * specified. It just means that the port isn't connected to
  793. * any external interface.
  794. */
  795. intf = of_graph_get_remote_port_parent(ep_node);
  796. if (!intf)
  797. continue;
  798. drm_of_component_match_add(master_dev, matchptr, compare_of,
  799. intf);
  800. of_node_put(intf);
  801. }
  802. return 0;
  803. }
  804. static int compare_name_mdp(struct device *dev, void *data)
  805. {
  806. return (strstr(dev_name(dev), "mdp") != NULL);
  807. }
  808. static int add_display_components(struct device *dev,
  809. struct component_match **matchptr)
  810. {
  811. struct device *mdp_dev;
  812. int ret;
  813. /*
  814. * MDP5 based devices don't have a flat hierarchy. There is a top level
  815. * parent: MDSS, and children: MDP5, DSI, HDMI, eDP etc. Populate the
  816. * children devices, find the MDP5 node, and then add the interfaces
  817. * to our components list.
  818. */
  819. if (of_device_is_compatible(dev->of_node, "qcom,mdss")) {
  820. ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
  821. if (ret) {
  822. dev_err(dev, "failed to populate children devices\n");
  823. return ret;
  824. }
  825. mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
  826. if (!mdp_dev) {
  827. dev_err(dev, "failed to find MDSS MDP node\n");
  828. of_platform_depopulate(dev);
  829. return -ENODEV;
  830. }
  831. put_device(mdp_dev);
  832. /* add the MDP component itself */
  833. drm_of_component_match_add(dev, matchptr, compare_of,
  834. mdp_dev->of_node);
  835. } else {
  836. /* MDP4 */
  837. mdp_dev = dev;
  838. }
  839. ret = add_components_mdp(mdp_dev, matchptr);
  840. if (ret)
  841. of_platform_depopulate(dev);
  842. return ret;
  843. }
  844. /*
  845. * We don't know what's the best binding to link the gpu with the drm device.
  846. * Fow now, we just hunt for all the possible gpus that we support, and add them
  847. * as components.
  848. */
  849. static const struct of_device_id msm_gpu_match[] = {
  850. { .compatible = "qcom,adreno" },
  851. { .compatible = "qcom,adreno-3xx" },
  852. { .compatible = "qcom,kgsl-3d0" },
  853. { },
  854. };
  855. static int add_gpu_components(struct device *dev,
  856. struct component_match **matchptr)
  857. {
  858. struct device_node *np;
  859. np = of_find_matching_node(NULL, msm_gpu_match);
  860. if (!np)
  861. return 0;
  862. drm_of_component_match_add(dev, matchptr, compare_of, np);
  863. of_node_put(np);
  864. return 0;
  865. }
  866. static int msm_drm_bind(struct device *dev)
  867. {
  868. return msm_drm_init(dev, &msm_driver);
  869. }
  870. static void msm_drm_unbind(struct device *dev)
  871. {
  872. msm_drm_uninit(dev);
  873. }
  874. static const struct component_master_ops msm_drm_ops = {
  875. .bind = msm_drm_bind,
  876. .unbind = msm_drm_unbind,
  877. };
  878. /*
  879. * Platform driver:
  880. */
  881. static int msm_pdev_probe(struct platform_device *pdev)
  882. {
  883. struct component_match *match = NULL;
  884. int ret;
  885. ret = add_display_components(&pdev->dev, &match);
  886. if (ret)
  887. return ret;
  888. ret = add_gpu_components(&pdev->dev, &match);
  889. if (ret)
  890. return ret;
  891. /* on all devices that I am aware of, iommu's which can map
  892. * any address the cpu can see are used:
  893. */
  894. ret = dma_set_mask_and_coherent(&pdev->dev, ~0);
  895. if (ret)
  896. return ret;
  897. return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
  898. }
  899. static int msm_pdev_remove(struct platform_device *pdev)
  900. {
  901. component_master_del(&pdev->dev, &msm_drm_ops);
  902. of_platform_depopulate(&pdev->dev);
  903. return 0;
  904. }
  905. static const struct of_device_id dt_match[] = {
  906. { .compatible = "qcom,mdp4", .data = (void *)4 }, /* MDP4 */
  907. { .compatible = "qcom,mdss", .data = (void *)5 }, /* MDP5 MDSS */
  908. {}
  909. };
  910. MODULE_DEVICE_TABLE(of, dt_match);
  911. static struct platform_driver msm_platform_driver = {
  912. .probe = msm_pdev_probe,
  913. .remove = msm_pdev_remove,
  914. .driver = {
  915. .name = "msm",
  916. .of_match_table = dt_match,
  917. .pm = &msm_pm_ops,
  918. },
  919. };
  920. static int __init msm_drm_register(void)
  921. {
  922. DBG("init");
  923. msm_mdp_register();
  924. msm_dsi_register();
  925. msm_edp_register();
  926. msm_hdmi_register();
  927. adreno_register();
  928. return platform_driver_register(&msm_platform_driver);
  929. }
  930. static void __exit msm_drm_unregister(void)
  931. {
  932. DBG("fini");
  933. platform_driver_unregister(&msm_platform_driver);
  934. msm_hdmi_unregister();
  935. adreno_unregister();
  936. msm_edp_unregister();
  937. msm_dsi_unregister();
  938. msm_mdp_unregister();
  939. }
  940. module_init(msm_drm_register);
  941. module_exit(msm_drm_unregister);
  942. MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
  943. MODULE_DESCRIPTION("MSM DRM Driver");
  944. MODULE_LICENSE("GPL");