0505-drm-vc4-Add-a-mode-for-using-the-closed-firmware-for.patch 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. From ceb205fa42f950c74172f9bbce65f1df3fdfdb34 Mon Sep 17 00:00:00 2001
  2. From: Eric Anholt <eric@anholt.net>
  3. Date: Wed, 14 Sep 2016 08:39:33 +0100
  4. Subject: [PATCH] drm/vc4: Add a mode for using the closed firmware for
  5. display.
  6. Signed-off-by: Eric Anholt <eric@anholt.net>
  7. ---
  8. drivers/gpu/drm/vc4/Makefile | 1 +
  9. drivers/gpu/drm/vc4/vc4_crtc.c | 13 +
  10. drivers/gpu/drm/vc4/vc4_drv.c | 10 +-
  11. drivers/gpu/drm/vc4/vc4_drv.h | 7 +
  12. drivers/gpu/drm/vc4/vc4_firmware_kms.c | 660 +++++++++++++++++++++++++++++++++
  13. 5 files changed, 689 insertions(+), 2 deletions(-)
  14. create mode 100644 drivers/gpu/drm/vc4/vc4_firmware_kms.c
  15. --- a/drivers/gpu/drm/vc4/Makefile
  16. +++ b/drivers/gpu/drm/vc4/Makefile
  17. @@ -9,6 +9,7 @@ vc4-y := \
  18. vc4_drv.o \
  19. vc4_dpi.o \
  20. vc4_dsi.o \
  21. + vc4_firmware_kms.o \
  22. vc4_kms.o \
  23. vc4_gem.o \
  24. vc4_hdmi.o \
  25. --- a/drivers/gpu/drm/vc4/vc4_crtc.c
  26. +++ b/drivers/gpu/drm/vc4/vc4_crtc.c
  27. @@ -151,6 +151,9 @@ int vc4_crtc_get_scanoutpos(struct drm_d
  28. int vblank_lines;
  29. int ret = 0;
  30. + if (vc4->firmware_kms)
  31. + return 0;
  32. +
  33. /*
  34. * XXX Doesn't work well in interlaced mode yet, partially due
  35. * to problems in vc4 kms or drm core interlaced mode handling,
  36. @@ -639,6 +642,11 @@ int vc4_enable_vblank(struct drm_device
  37. struct vc4_dev *vc4 = to_vc4_dev(dev);
  38. struct vc4_crtc *vc4_crtc = vc4->crtc[crtc_id];
  39. + if (vc4->firmware_kms) {
  40. + /* XXX: Can we mask the SMI interrupt? */
  41. + return 0;
  42. + }
  43. +
  44. CRTC_WRITE(PV_INTEN, PV_INT_VFP_START);
  45. return 0;
  46. @@ -649,6 +657,11 @@ void vc4_disable_vblank(struct drm_devic
  47. struct vc4_dev *vc4 = to_vc4_dev(dev);
  48. struct vc4_crtc *vc4_crtc = vc4->crtc[crtc_id];
  49. + if (vc4->firmware_kms) {
  50. + /* XXX: Can we mask the SMI interrupt? */
  51. + return;
  52. + }
  53. +
  54. CRTC_WRITE(PV_INTEN, 0);
  55. }
  56. --- a/drivers/gpu/drm/vc4/vc4_drv.c
  57. +++ b/drivers/gpu/drm/vc4/vc4_drv.c
  58. @@ -47,10 +47,15 @@ void __iomem *vc4_ioremap_regs(struct pl
  59. static void vc4_drm_preclose(struct drm_device *dev, struct drm_file *file)
  60. {
  61. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  62. struct drm_crtc *crtc;
  63. - list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  64. - vc4_cancel_page_flip(crtc, file);
  65. + list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  66. + if (vc4->firmware_kms)
  67. + vc4_fkms_cancel_page_flip(crtc, file);
  68. + else
  69. + vc4_cancel_page_flip(crtc, file);
  70. + }
  71. }
  72. void vc4_dump_regs32(const struct debugfs_reg32 *regs, unsigned int num_regs,
  73. @@ -331,6 +336,7 @@ static struct platform_driver *const com
  74. &vc4_dsi_driver,
  75. &vc4_hvs_driver,
  76. &vc4_crtc_driver,
  77. + &vc4_firmware_kms_driver,
  78. &vc4_v3d_driver,
  79. };
  80. --- a/drivers/gpu/drm/vc4/vc4_drv.h
  81. +++ b/drivers/gpu/drm/vc4/vc4_drv.h
  82. @@ -14,6 +14,9 @@ struct debugfs_reg32;
  83. struct vc4_dev {
  84. struct drm_device *dev;
  85. + bool firmware_kms;
  86. + struct rpi_firmware *firmware;
  87. +
  88. struct vc4_hdmi *hdmi;
  89. struct vc4_hvs *hvs;
  90. struct vc4_crtc *crtc[3];
  91. @@ -461,6 +464,10 @@ int vc4_dpi_debugfs_regs(struct seq_file
  92. extern struct platform_driver vc4_dsi_driver;
  93. int vc4_dsi_debugfs_regs(struct seq_file *m, void *unused);
  94. +/* vc4_firmware_kms.c */
  95. +extern struct platform_driver vc4_firmware_kms_driver;
  96. +void vc4_fkms_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file);
  97. +
  98. /* vc4_gem.c */
  99. void vc4_gem_init(struct drm_device *dev);
  100. void vc4_gem_destroy(struct drm_device *dev);
  101. --- /dev/null
  102. +++ b/drivers/gpu/drm/vc4/vc4_firmware_kms.c
  103. @@ -0,0 +1,660 @@
  104. +/*
  105. + * Copyright (C) 2016 Broadcom
  106. + *
  107. + * This program is free software; you can redistribute it and/or modify
  108. + * it under the terms of the GNU General Public License version 2 as
  109. + * published by the Free Software Foundation.
  110. + */
  111. +
  112. +/**
  113. + * DOC: VC4 firmware KMS module.
  114. + *
  115. + * As a hack to get us from the current closed source driver world
  116. + * toward a totally open stack, implement KMS on top of the Raspberry
  117. + * Pi's firmware display stack.
  118. + */
  119. +
  120. +#include "drm_atomic.h"
  121. +#include "drm_atomic_helper.h"
  122. +#include "drm_plane_helper.h"
  123. +#include "drm_crtc_helper.h"
  124. +#include "linux/clk.h"
  125. +#include "linux/debugfs.h"
  126. +#include "drm_fb_cma_helper.h"
  127. +#include "linux/component.h"
  128. +#include "linux/of_device.h"
  129. +#include "vc4_drv.h"
  130. +#include "vc4_regs.h"
  131. +#include <soc/bcm2835/raspberrypi-firmware.h>
  132. +
  133. +/* The firmware delivers a vblank interrupt to us through the SMI
  134. + * hardware, which has only this one register.
  135. + */
  136. +#define SMICS 0x0
  137. +#define SMICS_INTERRUPTS (BIT(9) | BIT(10) | BIT(11))
  138. +
  139. +struct vc4_crtc {
  140. + struct drm_crtc base;
  141. + struct drm_encoder *encoder;
  142. + struct drm_connector *connector;
  143. + void __iomem *regs;
  144. +
  145. + struct drm_pending_vblank_event *event;
  146. +};
  147. +
  148. +static inline struct vc4_crtc *to_vc4_crtc(struct drm_crtc *crtc)
  149. +{
  150. + return container_of(crtc, struct vc4_crtc, base);
  151. +}
  152. +
  153. +struct vc4_fkms_encoder {
  154. + struct drm_encoder base;
  155. +};
  156. +
  157. +static inline struct vc4_fkms_encoder *
  158. +to_vc4_fkms_encoder(struct drm_encoder *encoder)
  159. +{
  160. + return container_of(encoder, struct vc4_fkms_encoder, base);
  161. +}
  162. +
  163. +/* VC4 FKMS connector KMS struct */
  164. +struct vc4_fkms_connector {
  165. + struct drm_connector base;
  166. +
  167. + /* Since the connector is attached to just the one encoder,
  168. + * this is the reference to it so we can do the best_encoder()
  169. + * hook.
  170. + */
  171. + struct drm_encoder *encoder;
  172. +};
  173. +
  174. +static inline struct vc4_fkms_connector *
  175. +to_vc4_fkms_connector(struct drm_connector *connector)
  176. +{
  177. + return container_of(connector, struct vc4_fkms_connector, base);
  178. +}
  179. +
  180. +/* Firmware's structure for making an FB mbox call. */
  181. +struct fbinfo_s {
  182. + u32 xres, yres, xres_virtual, yres_virtual;
  183. + u32 pitch, bpp;
  184. + u32 xoffset, yoffset;
  185. + u32 base;
  186. + u32 screen_size;
  187. + u16 cmap[256];
  188. +};
  189. +
  190. +struct vc4_fkms_plane {
  191. + struct drm_plane base;
  192. + struct fbinfo_s *fbinfo;
  193. + dma_addr_t fbinfo_bus_addr;
  194. + u32 pitch;
  195. +};
  196. +
  197. +static inline struct vc4_fkms_plane *to_vc4_fkms_plane(struct drm_plane *plane)
  198. +{
  199. + return (struct vc4_fkms_plane *)plane;
  200. +}
  201. +
  202. +/* Turns the display on/off. */
  203. +static int vc4_plane_set_primary_blank(struct drm_plane *plane, bool blank)
  204. +{
  205. + struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
  206. +
  207. + u32 packet = blank;
  208. + return rpi_firmware_property(vc4->firmware,
  209. + RPI_FIRMWARE_FRAMEBUFFER_BLANK,
  210. + &packet, sizeof(packet));
  211. +}
  212. +
  213. +static void vc4_primary_plane_atomic_update(struct drm_plane *plane,
  214. + struct drm_plane_state *old_state)
  215. +{
  216. + struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
  217. + struct vc4_fkms_plane *vc4_plane = to_vc4_fkms_plane(plane);
  218. + struct drm_plane_state *state = plane->state;
  219. + struct drm_framebuffer *fb = state->fb;
  220. + struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
  221. + volatile struct fbinfo_s *fbinfo = vc4_plane->fbinfo;
  222. + u32 bpp = 32;
  223. + int ret;
  224. +
  225. + vc4_plane_set_primary_blank(plane, false);
  226. +
  227. + fbinfo->xres = state->crtc_w;
  228. + fbinfo->yres = state->crtc_h;
  229. + fbinfo->xres_virtual = state->crtc_w;
  230. + fbinfo->yres_virtual = state->crtc_h;
  231. + fbinfo->bpp = bpp;
  232. + fbinfo->xoffset = state->crtc_x;
  233. + fbinfo->yoffset = state->crtc_y;
  234. + fbinfo->base = bo->paddr + fb->offsets[0];
  235. + fbinfo->pitch = fb->pitches[0];
  236. + /* A bug in the firmware makes it so that if the fb->base is
  237. + * set to nonzero, the configured pitch gets overwritten with
  238. + * the previous pitch. So, to get the configured pitch
  239. + * recomputed, we have to make it allocate itself a new buffer
  240. + * in VC memory, first.
  241. + */
  242. + if (vc4_plane->pitch != fb->pitches[0]) {
  243. + u32 saved_base = fbinfo->base;
  244. + fbinfo->base = 0;
  245. +
  246. + ret = rpi_firmware_transaction(vc4->firmware,
  247. + RPI_FIRMWARE_CHAN_FB,
  248. + vc4_plane->fbinfo_bus_addr);
  249. + fbinfo->base = saved_base;
  250. +
  251. + vc4_plane->pitch = fbinfo->pitch;
  252. + WARN_ON_ONCE(vc4_plane->pitch != fb->pitches[0]);
  253. + }
  254. +
  255. + ret = rpi_firmware_transaction(vc4->firmware,
  256. + RPI_FIRMWARE_CHAN_FB,
  257. + vc4_plane->fbinfo_bus_addr);
  258. + WARN_ON_ONCE(fbinfo->pitch != fb->pitches[0]);
  259. + WARN_ON_ONCE(fbinfo->base != bo->paddr + fb->offsets[0]);
  260. +}
  261. +
  262. +static void vc4_primary_plane_atomic_disable(struct drm_plane *plane,
  263. + struct drm_plane_state *old_state)
  264. +{
  265. + vc4_plane_set_primary_blank(plane, true);
  266. +}
  267. +
  268. +static void vc4_cursor_plane_atomic_update(struct drm_plane *plane,
  269. + struct drm_plane_state *old_state)
  270. +{
  271. + struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
  272. + struct drm_plane_state *state = plane->state;
  273. + struct drm_framebuffer *fb = state->fb;
  274. + struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
  275. + int ret;
  276. + u32 packet_state[] = { true, state->crtc_x, state->crtc_y, 0 };
  277. + u32 packet_info[] = { state->crtc_w, state->crtc_h,
  278. + 0, /* unused */
  279. + bo->paddr + fb->offsets[0],
  280. + 0, 0, /* hotx, hoty */};
  281. + WARN_ON_ONCE(fb->pitches[0] != state->crtc_w * 4);
  282. + WARN_ON_ONCE(fb->bits_per_pixel != 32);
  283. +
  284. + ret = rpi_firmware_property(vc4->firmware,
  285. + RPI_FIRMWARE_SET_CURSOR_STATE,
  286. + &packet_state,
  287. + sizeof(packet_state));
  288. + if (ret || packet_state[0] != 0)
  289. + DRM_ERROR("Failed to set cursor state: 0x%08x\n", packet_state[0]);
  290. +
  291. + ret = rpi_firmware_property(vc4->firmware,
  292. + RPI_FIRMWARE_SET_CURSOR_INFO,
  293. + &packet_info,
  294. + sizeof(packet_info));
  295. + if (ret || packet_info[0] != 0)
  296. + DRM_ERROR("Failed to set cursor info: 0x%08x\n", packet_info[0]);
  297. +}
  298. +
  299. +static void vc4_cursor_plane_atomic_disable(struct drm_plane *plane,
  300. + struct drm_plane_state *old_state)
  301. +{
  302. + struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
  303. + u32 packet_state[] = { false, 0, 0, 0 };
  304. + int ret;
  305. +
  306. + ret = rpi_firmware_property(vc4->firmware,
  307. + RPI_FIRMWARE_SET_CURSOR_STATE,
  308. + &packet_state,
  309. + sizeof(packet_state));
  310. + if (ret || packet_state[0] != 0)
  311. + DRM_ERROR("Failed to set cursor state: 0x%08x\n", packet_state[0]);
  312. +}
  313. +
  314. +static int vc4_plane_atomic_check(struct drm_plane *plane,
  315. + struct drm_plane_state *state)
  316. +{
  317. + return 0;
  318. +}
  319. +
  320. +static void vc4_plane_destroy(struct drm_plane *plane)
  321. +{
  322. + drm_plane_helper_disable(plane);
  323. + drm_plane_cleanup(plane);
  324. +}
  325. +
  326. +static const struct drm_plane_funcs vc4_plane_funcs = {
  327. + .update_plane = drm_atomic_helper_update_plane,
  328. + .disable_plane = drm_atomic_helper_disable_plane,
  329. + .destroy = vc4_plane_destroy,
  330. + .set_property = NULL,
  331. + .reset = drm_atomic_helper_plane_reset,
  332. + .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  333. + .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  334. +};
  335. +
  336. +static const struct drm_plane_helper_funcs vc4_primary_plane_helper_funcs = {
  337. + .prepare_fb = NULL,
  338. + .cleanup_fb = NULL,
  339. + .atomic_check = vc4_plane_atomic_check,
  340. + .atomic_update = vc4_primary_plane_atomic_update,
  341. + .atomic_disable = vc4_primary_plane_atomic_disable,
  342. +};
  343. +
  344. +static const struct drm_plane_helper_funcs vc4_cursor_plane_helper_funcs = {
  345. + .prepare_fb = NULL,
  346. + .cleanup_fb = NULL,
  347. + .atomic_check = vc4_plane_atomic_check,
  348. + .atomic_update = vc4_cursor_plane_atomic_update,
  349. + .atomic_disable = vc4_cursor_plane_atomic_disable,
  350. +};
  351. +
  352. +static struct drm_plane *vc4_fkms_plane_init(struct drm_device *dev,
  353. + enum drm_plane_type type)
  354. +{
  355. + struct drm_plane *plane = NULL;
  356. + struct vc4_fkms_plane *vc4_plane;
  357. + u32 xrgb8888 = DRM_FORMAT_XRGB8888;
  358. + u32 argb8888 = DRM_FORMAT_ARGB8888;
  359. + int ret = 0;
  360. + bool primary = (type == DRM_PLANE_TYPE_PRIMARY);
  361. +
  362. + vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
  363. + GFP_KERNEL);
  364. + if (!vc4_plane) {
  365. + ret = -ENOMEM;
  366. + goto fail;
  367. + }
  368. +
  369. + plane = &vc4_plane->base;
  370. + ret = drm_universal_plane_init(dev, plane, 0xff,
  371. + &vc4_plane_funcs,
  372. + primary ? &xrgb8888 : &argb8888, 1,
  373. + type);
  374. +
  375. + if (type == DRM_PLANE_TYPE_PRIMARY) {
  376. + vc4_plane->fbinfo =
  377. + dma_alloc_coherent(dev->dev,
  378. + sizeof(*vc4_plane->fbinfo),
  379. + &vc4_plane->fbinfo_bus_addr,
  380. + GFP_KERNEL);
  381. + memset(vc4_plane->fbinfo, 0, sizeof(*vc4_plane->fbinfo));
  382. +
  383. + drm_plane_helper_add(plane, &vc4_primary_plane_helper_funcs);
  384. + } else {
  385. + drm_plane_helper_add(plane, &vc4_cursor_plane_helper_funcs);
  386. + }
  387. +
  388. + return plane;
  389. +fail:
  390. + if (plane)
  391. + vc4_plane_destroy(plane);
  392. +
  393. + return ERR_PTR(ret);
  394. +}
  395. +
  396. +static void vc4_crtc_mode_set_nofb(struct drm_crtc *crtc)
  397. +{
  398. + /* Everyting is handled in the planes. */
  399. +}
  400. +
  401. +static void vc4_crtc_disable(struct drm_crtc *crtc)
  402. +{
  403. +}
  404. +
  405. +static void vc4_crtc_enable(struct drm_crtc *crtc)
  406. +{
  407. +}
  408. +
  409. +static int vc4_crtc_atomic_check(struct drm_crtc *crtc,
  410. + struct drm_crtc_state *state)
  411. +{
  412. + return 0;
  413. +}
  414. +
  415. +static void vc4_crtc_atomic_flush(struct drm_crtc *crtc,
  416. + struct drm_crtc_state *old_state)
  417. +{
  418. +}
  419. +
  420. +static void vc4_crtc_handle_page_flip(struct vc4_crtc *vc4_crtc)
  421. +{
  422. + struct drm_crtc *crtc = &vc4_crtc->base;
  423. + struct drm_device *dev = crtc->dev;
  424. + unsigned long flags;
  425. +
  426. + spin_lock_irqsave(&dev->event_lock, flags);
  427. + if (vc4_crtc->event) {
  428. + drm_crtc_send_vblank_event(crtc, vc4_crtc->event);
  429. + vc4_crtc->event = NULL;
  430. + drm_crtc_vblank_put(crtc);
  431. + }
  432. + spin_unlock_irqrestore(&dev->event_lock, flags);
  433. +}
  434. +
  435. +static irqreturn_t vc4_crtc_irq_handler(int irq, void *data)
  436. +{
  437. + struct vc4_crtc *vc4_crtc = data;
  438. + u32 stat = readl(vc4_crtc->regs + SMICS);
  439. + irqreturn_t ret = IRQ_NONE;
  440. +
  441. + if (stat & SMICS_INTERRUPTS) {
  442. + writel(0, vc4_crtc->regs + SMICS);
  443. + drm_crtc_handle_vblank(&vc4_crtc->base);
  444. + vc4_crtc_handle_page_flip(vc4_crtc);
  445. + ret = IRQ_HANDLED;
  446. + }
  447. +
  448. + return ret;
  449. +}
  450. +
  451. +static int vc4_page_flip(struct drm_crtc *crtc,
  452. + struct drm_framebuffer *fb,
  453. + struct drm_pending_vblank_event *event,
  454. + uint32_t flags)
  455. +{
  456. + if (flags & DRM_MODE_PAGE_FLIP_ASYNC) {
  457. + DRM_ERROR("Async flips aren't allowed\n");
  458. + return -EINVAL;
  459. + }
  460. +
  461. + return drm_atomic_helper_page_flip(crtc, fb, event, flags);
  462. +}
  463. +
  464. +static const struct drm_crtc_funcs vc4_crtc_funcs = {
  465. + .set_config = drm_atomic_helper_set_config,
  466. + .destroy = drm_crtc_cleanup,
  467. + .page_flip = vc4_page_flip,
  468. + .set_property = NULL,
  469. + .cursor_set = NULL, /* handled by drm_mode_cursor_universal */
  470. + .cursor_move = NULL, /* handled by drm_mode_cursor_universal */
  471. + .reset = drm_atomic_helper_crtc_reset,
  472. + .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  473. + .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  474. +};
  475. +
  476. +static const struct drm_crtc_helper_funcs vc4_crtc_helper_funcs = {
  477. + .mode_set_nofb = vc4_crtc_mode_set_nofb,
  478. + .disable = vc4_crtc_disable,
  479. + .enable = vc4_crtc_enable,
  480. + .atomic_check = vc4_crtc_atomic_check,
  481. + .atomic_flush = vc4_crtc_atomic_flush,
  482. +};
  483. +
  484. +/* Frees the page flip event when the DRM device is closed with the
  485. + * event still outstanding.
  486. + */
  487. +void vc4_fkms_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  488. +{
  489. + struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  490. + struct drm_device *dev = crtc->dev;
  491. + unsigned long flags;
  492. +
  493. + spin_lock_irqsave(&dev->event_lock, flags);
  494. +
  495. + if (vc4_crtc->event && vc4_crtc->event->base.file_priv == file) {
  496. + vc4_crtc->event->base.destroy(&vc4_crtc->event->base);
  497. + drm_crtc_vblank_put(crtc);
  498. + vc4_crtc->event = NULL;
  499. + }
  500. +
  501. + spin_unlock_irqrestore(&dev->event_lock, flags);
  502. +}
  503. +
  504. +static const struct of_device_id vc4_firmware_kms_dt_match[] = {
  505. + { .compatible = "raspberrypi,rpi-firmware-kms" },
  506. + {}
  507. +};
  508. +
  509. +static enum drm_connector_status
  510. +vc4_fkms_connector_detect(struct drm_connector *connector, bool force)
  511. +{
  512. + return connector_status_connected;
  513. +}
  514. +
  515. +static int vc4_fkms_connector_get_modes(struct drm_connector *connector)
  516. +{
  517. + struct drm_device *dev = connector->dev;
  518. + struct vc4_dev *vc4 = to_vc4_dev(dev);
  519. + u32 wh[2] = {0, 0};
  520. + int ret;
  521. + struct drm_display_mode *mode;
  522. +
  523. + ret = rpi_firmware_property(vc4->firmware,
  524. + RPI_FIRMWARE_FRAMEBUFFER_GET_PHYSICAL_WIDTH_HEIGHT,
  525. + &wh, sizeof(wh));
  526. + if (ret) {
  527. + DRM_ERROR("Failed to get screen size: %d (0x%08x 0x%08x)\n",
  528. + ret, wh[0], wh[1]);
  529. + return 0;
  530. + }
  531. +
  532. + mode = drm_cvt_mode(dev, wh[0], wh[1], 60 /* vrefresh */,
  533. + 0, 0, false);
  534. + drm_mode_probed_add(connector, mode);
  535. +
  536. + return 1;
  537. +}
  538. +
  539. +static struct drm_encoder *
  540. +vc4_fkms_connector_best_encoder(struct drm_connector *connector)
  541. +{
  542. + struct vc4_fkms_connector *fkms_connector =
  543. + to_vc4_fkms_connector(connector);
  544. + return fkms_connector->encoder;
  545. +}
  546. +
  547. +static void vc4_fkms_connector_destroy(struct drm_connector *connector)
  548. +{
  549. + drm_connector_unregister(connector);
  550. + drm_connector_cleanup(connector);
  551. +}
  552. +
  553. +static const struct drm_connector_funcs vc4_fkms_connector_funcs = {
  554. + .dpms = drm_atomic_helper_connector_dpms,
  555. + .detect = vc4_fkms_connector_detect,
  556. + .fill_modes = drm_helper_probe_single_connector_modes,
  557. + .destroy = vc4_fkms_connector_destroy,
  558. + .reset = drm_atomic_helper_connector_reset,
  559. + .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  560. + .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  561. +};
  562. +
  563. +static const struct drm_connector_helper_funcs vc4_fkms_connector_helper_funcs = {
  564. + .get_modes = vc4_fkms_connector_get_modes,
  565. + .best_encoder = vc4_fkms_connector_best_encoder,
  566. +};
  567. +
  568. +static struct drm_connector *vc4_fkms_connector_init(struct drm_device *dev,
  569. + struct drm_encoder *encoder)
  570. +{
  571. + struct drm_connector *connector = NULL;
  572. + struct vc4_fkms_connector *fkms_connector;
  573. + int ret = 0;
  574. +
  575. + fkms_connector = devm_kzalloc(dev->dev, sizeof(*fkms_connector),
  576. + GFP_KERNEL);
  577. + if (!fkms_connector) {
  578. + ret = -ENOMEM;
  579. + goto fail;
  580. + }
  581. + connector = &fkms_connector->base;
  582. +
  583. + fkms_connector->encoder = encoder;
  584. +
  585. + drm_connector_init(dev, connector, &vc4_fkms_connector_funcs,
  586. + DRM_MODE_CONNECTOR_HDMIA);
  587. + drm_connector_helper_add(connector, &vc4_fkms_connector_helper_funcs);
  588. +
  589. + connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
  590. + DRM_CONNECTOR_POLL_DISCONNECT);
  591. +
  592. + connector->interlace_allowed = 0;
  593. + connector->doublescan_allowed = 0;
  594. +
  595. + drm_mode_connector_attach_encoder(connector, encoder);
  596. +
  597. + return connector;
  598. +
  599. + fail:
  600. + if (connector)
  601. + vc4_fkms_connector_destroy(connector);
  602. +
  603. + return ERR_PTR(ret);
  604. +}
  605. +
  606. +static void vc4_fkms_encoder_destroy(struct drm_encoder *encoder)
  607. +{
  608. + drm_encoder_cleanup(encoder);
  609. +}
  610. +
  611. +static const struct drm_encoder_funcs vc4_fkms_encoder_funcs = {
  612. + .destroy = vc4_fkms_encoder_destroy,
  613. +};
  614. +
  615. +static void vc4_fkms_encoder_enable(struct drm_encoder *encoder)
  616. +{
  617. +}
  618. +
  619. +static void vc4_fkms_encoder_disable(struct drm_encoder *encoder)
  620. +{
  621. +}
  622. +
  623. +static const struct drm_encoder_helper_funcs vc4_fkms_encoder_helper_funcs = {
  624. + .enable = vc4_fkms_encoder_enable,
  625. + .disable = vc4_fkms_encoder_disable,
  626. +};
  627. +
  628. +static int vc4_fkms_bind(struct device *dev, struct device *master, void *data)
  629. +{
  630. + struct platform_device *pdev = to_platform_device(dev);
  631. + struct drm_device *drm = dev_get_drvdata(master);
  632. + struct vc4_dev *vc4 = to_vc4_dev(drm);
  633. + struct vc4_crtc *vc4_crtc;
  634. + struct vc4_fkms_encoder *vc4_encoder;
  635. + struct drm_crtc *crtc;
  636. + struct drm_plane *primary_plane, *cursor_plane, *destroy_plane, *temp;
  637. + struct device_node *firmware_node;
  638. + int ret;
  639. +
  640. + vc4->firmware_kms = true;
  641. +
  642. + vc4_crtc = devm_kzalloc(dev, sizeof(*vc4_crtc), GFP_KERNEL);
  643. + if (!vc4_crtc)
  644. + return -ENOMEM;
  645. + crtc = &vc4_crtc->base;
  646. +
  647. + firmware_node = of_parse_phandle(dev->of_node, "brcm,firmware", 0);
  648. + vc4->firmware = rpi_firmware_get(firmware_node);
  649. + if (!vc4->firmware) {
  650. + DRM_DEBUG("Failed to get Raspberry Pi firmware reference.\n");
  651. + return -EPROBE_DEFER;
  652. + }
  653. + of_node_put(firmware_node);
  654. +
  655. + /* Map the SMI interrupt reg */
  656. + vc4_crtc->regs = vc4_ioremap_regs(pdev, 0);
  657. + if (IS_ERR(vc4_crtc->regs))
  658. + return PTR_ERR(vc4_crtc->regs);
  659. +
  660. + /* For now, we create just the primary and the legacy cursor
  661. + * planes. We should be able to stack more planes on easily,
  662. + * but to do that we would need to compute the bandwidth
  663. + * requirement of the plane configuration, and reject ones
  664. + * that will take too much.
  665. + */
  666. + primary_plane = vc4_fkms_plane_init(drm, DRM_PLANE_TYPE_PRIMARY);
  667. + if (IS_ERR(primary_plane)) {
  668. + dev_err(dev, "failed to construct primary plane\n");
  669. + ret = PTR_ERR(primary_plane);
  670. + goto err;
  671. + }
  672. +
  673. + cursor_plane = vc4_fkms_plane_init(drm, DRM_PLANE_TYPE_CURSOR);
  674. + if (IS_ERR(cursor_plane)) {
  675. + dev_err(dev, "failed to construct cursor plane\n");
  676. + ret = PTR_ERR(cursor_plane);
  677. + goto err;
  678. + }
  679. +
  680. + drm_crtc_init_with_planes(drm, crtc, primary_plane, cursor_plane,
  681. + &vc4_crtc_funcs);
  682. + drm_crtc_helper_add(crtc, &vc4_crtc_helper_funcs);
  683. + primary_plane->crtc = crtc;
  684. + cursor_plane->crtc = crtc;
  685. + vc4->crtc[drm_crtc_index(crtc)] = vc4_crtc;
  686. +
  687. + vc4_encoder = devm_kzalloc(dev, sizeof(*vc4_encoder), GFP_KERNEL);
  688. + if (!vc4_encoder)
  689. + return -ENOMEM;
  690. + vc4_crtc->encoder = &vc4_encoder->base;
  691. + vc4_encoder->base.possible_crtcs |= drm_crtc_mask(crtc) ;
  692. + drm_encoder_init(drm, &vc4_encoder->base, &vc4_fkms_encoder_funcs,
  693. + DRM_MODE_ENCODER_TMDS);
  694. + drm_encoder_helper_add(&vc4_encoder->base,
  695. + &vc4_fkms_encoder_helper_funcs);
  696. +
  697. + vc4_crtc->connector = vc4_fkms_connector_init(drm, &vc4_encoder->base);
  698. + if (IS_ERR(vc4_crtc->connector)) {
  699. + ret = PTR_ERR(vc4_crtc->connector);
  700. + goto err_destroy_encoder;
  701. + }
  702. +
  703. + writel(0, vc4_crtc->regs + SMICS);
  704. + ret = devm_request_irq(dev, platform_get_irq(pdev, 0),
  705. + vc4_crtc_irq_handler, 0, "vc4 firmware kms",
  706. + vc4_crtc);
  707. + if (ret)
  708. + goto err_destroy_connector;
  709. +
  710. + platform_set_drvdata(pdev, vc4_crtc);
  711. +
  712. + return 0;
  713. +
  714. +err_destroy_connector:
  715. + vc4_fkms_connector_destroy(vc4_crtc->connector);
  716. +err_destroy_encoder:
  717. + vc4_fkms_encoder_destroy(vc4_crtc->encoder);
  718. + list_for_each_entry_safe(destroy_plane, temp,
  719. + &drm->mode_config.plane_list, head) {
  720. + if (destroy_plane->possible_crtcs == 1 << drm_crtc_index(crtc))
  721. + destroy_plane->funcs->destroy(destroy_plane);
  722. + }
  723. +err:
  724. + return ret;
  725. +}
  726. +
  727. +static void vc4_fkms_unbind(struct device *dev, struct device *master,
  728. + void *data)
  729. +{
  730. + struct platform_device *pdev = to_platform_device(dev);
  731. + struct vc4_crtc *vc4_crtc = dev_get_drvdata(dev);
  732. +
  733. + vc4_fkms_connector_destroy(vc4_crtc->connector);
  734. + vc4_fkms_encoder_destroy(vc4_crtc->encoder);
  735. + drm_crtc_cleanup(&vc4_crtc->base);
  736. +
  737. + platform_set_drvdata(pdev, NULL);
  738. +}
  739. +
  740. +static const struct component_ops vc4_fkms_ops = {
  741. + .bind = vc4_fkms_bind,
  742. + .unbind = vc4_fkms_unbind,
  743. +};
  744. +
  745. +static int vc4_fkms_probe(struct platform_device *pdev)
  746. +{
  747. + return component_add(&pdev->dev, &vc4_fkms_ops);
  748. +}
  749. +
  750. +static int vc4_fkms_remove(struct platform_device *pdev)
  751. +{
  752. + component_del(&pdev->dev, &vc4_fkms_ops);
  753. + return 0;
  754. +}
  755. +
  756. +struct platform_driver vc4_firmware_kms_driver = {
  757. + .probe = vc4_fkms_probe,
  758. + .remove = vc4_fkms_remove,
  759. + .driver = {
  760. + .name = "vc4_firmware_kms",
  761. + .of_match_table = vc4_firmware_kms_dt_match,
  762. + },
  763. +};