1 /* 2 * Copyright (C) 2015 Broadcom 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 as 6 * published by the Free Software Foundation. 7 */ 8 9 /** 10 * DOC: VC4 KMS 11 * 12 * This is the general code for implementing KMS mode setting that 13 * doesn't clearly associate with any of the other objects (plane, 14 * crtc, HDMI encoder). 15 */ 16 17 #include "drm_crtc.h" 18 #include "drm_atomic.h" 19 #include "drm_atomic_helper.h" 20 #include "drm_crtc_helper.h" 21 #include "drm_plane_helper.h" 22 #include "drm_fb_cma_helper.h" 23 #include "vc4_drv.h" 24 25 static void vc4_output_poll_changed(struct drm_device *dev) 26 { 27 struct vc4_dev *vc4 = to_vc4_dev(dev); 28 29 if (vc4->fbdev) 30 drm_fbdev_cma_hotplug_event(vc4->fbdev); 31 } 32 33 struct vc4_commit { 34 struct drm_device *dev; 35 struct drm_atomic_state *state; 36 struct vc4_seqno_cb cb; 37 }; 38 39 static void 40 vc4_atomic_complete_commit(struct vc4_commit *c) 41 { 42 struct drm_atomic_state *state = c->state; 43 struct drm_device *dev = state->dev; 44 struct vc4_dev *vc4 = to_vc4_dev(dev); 45 46 drm_atomic_helper_commit_modeset_disables(dev, state); 47 48 drm_atomic_helper_commit_planes(dev, state, false); 49 50 drm_atomic_helper_commit_modeset_enables(dev, state); 51 52 drm_atomic_helper_wait_for_vblanks(dev, state); 53 54 drm_atomic_helper_cleanup_planes(dev, state); 55 56 drm_atomic_state_free(state); 57 58 up(&vc4->async_modeset); 59 60 kfree(c); 61 } 62 63 static void 64 vc4_atomic_complete_commit_seqno_cb(struct vc4_seqno_cb *cb) 65 { 66 struct vc4_commit *c = container_of(cb, struct vc4_commit, cb); 67 68 vc4_atomic_complete_commit(c); 69 } 70 71 static struct vc4_commit *commit_init(struct drm_atomic_state *state) 72 { 73 struct vc4_commit *c = kzalloc(sizeof(*c), GFP_KERNEL); 74 75 if (!c) 76 return NULL; 77 c->dev = state->dev; 78 c->state = state; 79 80 return c; 81 } 82 83 /** 84 * vc4_atomic_commit - commit validated state object 85 * @dev: DRM device 86 * @state: the driver state object 87 * @async: asynchronous commit 88 * 89 * This function commits a with drm_atomic_helper_check() pre-validated state 90 * object. This can still fail when e.g. the framebuffer reservation fails. For 91 * now this doesn't implement asynchronous commits. 92 * 93 * RETURNS 94 * Zero for success or -errno. 95 */ 96 static int vc4_atomic_commit(struct drm_device *dev, 97 struct drm_atomic_state *state, 98 bool async) 99 { 100 struct vc4_dev *vc4 = to_vc4_dev(dev); 101 int ret; 102 int i; 103 uint64_t wait_seqno = 0; 104 struct vc4_commit *c; 105 106 c = commit_init(state); 107 if (!c) 108 return -ENOMEM; 109 110 /* Make sure that any outstanding modesets have finished. */ 111 ret = down_interruptible(&vc4->async_modeset); 112 if (ret) { 113 kfree(c); 114 return ret; 115 } 116 117 ret = drm_atomic_helper_prepare_planes(dev, state); 118 if (ret) { 119 kfree(c); 120 up(&vc4->async_modeset); 121 return ret; 122 } 123 124 for (i = 0; i < dev->mode_config.num_total_plane; i++) { 125 struct drm_plane *plane = state->planes[i]; 126 struct drm_plane_state *new_state = state->plane_states[i]; 127 128 if (!plane) 129 continue; 130 131 if ((plane->state->fb != new_state->fb) && new_state->fb) { 132 struct drm_gem_cma_object *cma_bo = 133 drm_fb_cma_get_gem_obj(new_state->fb, 0); 134 struct vc4_bo *bo = to_vc4_bo(&cma_bo->base); 135 136 wait_seqno = max(bo->seqno, wait_seqno); 137 } 138 } 139 140 /* 141 * This is the point of no return - everything below never fails except 142 * when the hw goes bonghits. Which means we can commit the new state on 143 * the software side now. 144 */ 145 146 drm_atomic_helper_swap_state(dev, state); 147 148 /* 149 * Everything below can be run asynchronously without the need to grab 150 * any modeset locks at all under one condition: It must be guaranteed 151 * that the asynchronous work has either been cancelled (if the driver 152 * supports it, which at least requires that the framebuffers get 153 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed 154 * before the new state gets committed on the software side with 155 * drm_atomic_helper_swap_state(). 156 * 157 * This scheme allows new atomic state updates to be prepared and 158 * checked in parallel to the asynchronous completion of the previous 159 * update. Which is important since compositors need to figure out the 160 * composition of the next frame right after having submitted the 161 * current layout. 162 */ 163 164 if (async) { 165 vc4_queue_seqno_cb(dev, &c->cb, wait_seqno, 166 vc4_atomic_complete_commit_seqno_cb); 167 } else { 168 vc4_wait_for_seqno(dev, wait_seqno, ~0ull, false); 169 vc4_atomic_complete_commit(c); 170 } 171 172 return 0; 173 } 174 175 static const struct drm_mode_config_funcs vc4_mode_funcs = { 176 .output_poll_changed = vc4_output_poll_changed, 177 .atomic_check = drm_atomic_helper_check, 178 .atomic_commit = vc4_atomic_commit, 179 .fb_create = drm_fb_cma_create, 180 }; 181 182 int vc4_kms_load(struct drm_device *dev) 183 { 184 struct vc4_dev *vc4 = to_vc4_dev(dev); 185 int ret; 186 187 sema_init(&vc4->async_modeset, 1); 188 189 ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 190 if (ret < 0) { 191 dev_err(dev->dev, "failed to initialize vblank\n"); 192 return ret; 193 } 194 195 dev->mode_config.max_width = 2048; 196 dev->mode_config.max_height = 2048; 197 dev->mode_config.funcs = &vc4_mode_funcs; 198 dev->mode_config.preferred_depth = 24; 199 dev->mode_config.async_page_flip = true; 200 201 dev->vblank_disable_allowed = true; 202 203 drm_mode_config_reset(dev); 204 205 vc4->fbdev = drm_fbdev_cma_init(dev, 32, 206 dev->mode_config.num_crtc, 207 dev->mode_config.num_connector); 208 if (IS_ERR(vc4->fbdev)) 209 vc4->fbdev = NULL; 210 211 drm_kms_helper_poll_init(dev); 212 213 return 0; 214 } 215