2 * Copyright (C) 2008 Maarten Maathuis.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <acpi/video.h>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_atomic_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_fb_helper.h>
33 #include <drm/drm_fourcc.h>
34 #include <drm/drm_gem_framebuffer_helper.h>
35 #include <drm/drm_probe_helper.h>
36 #include <drm/drm_vblank.h>
38 #include "nouveau_fbcon.h"
39 #include "nouveau_crtc.h"
40 #include "nouveau_gem.h"
41 #include "nouveau_connector.h"
42 #include "nv50_display.h"
44 #include <nvif/class.h>
45 #include <nvif/cl0046.h>
46 #include <nvif/event.h>
47 #include <dispnv50/crc.h>
50 nouveau_display_vblank_enable(struct drm_crtc *crtc)
52 struct nouveau_crtc *nv_crtc;
54 nv_crtc = nouveau_crtc(crtc);
55 nvif_notify_get(&nv_crtc->vblank);
61 nouveau_display_vblank_disable(struct drm_crtc *crtc)
63 struct nouveau_crtc *nv_crtc;
65 nv_crtc = nouveau_crtc(crtc);
66 nvif_notify_put(&nv_crtc->vblank);
70 calc(int blanks, int blanke, int total, int line)
72 if (blanke >= blanks) {
84 nouveau_display_scanoutpos_head(struct drm_crtc *crtc, int *vpos, int *hpos,
85 ktime_t *stime, ktime_t *etime)
88 struct nv04_disp_mthd_v0 base;
89 struct nv04_disp_scanoutpos_v0 scan;
91 .base.method = NV04_DISP_SCANOUTPOS,
92 .base.head = nouveau_crtc(crtc)->index,
94 struct nouveau_display *disp = nouveau_display(crtc->dev);
95 struct drm_vblank_crtc *vblank = &crtc->dev->vblank[drm_crtc_index(crtc)];
100 ret = nvif_mthd(&disp->disp.object, 0, &args, sizeof(args));
104 if (args.scan.vline) {
109 if (retry) ndelay(vblank->linedur_ns);
112 *hpos = args.scan.hline;
113 *vpos = calc(args.scan.vblanks, args.scan.vblanke,
114 args.scan.vtotal, args.scan.vline);
115 if (stime) *stime = ns_to_ktime(args.scan.time[0]);
116 if (etime) *etime = ns_to_ktime(args.scan.time[1]);
122 nouveau_display_scanoutpos(struct drm_crtc *crtc,
123 bool in_vblank_irq, int *vpos, int *hpos,
124 ktime_t *stime, ktime_t *etime,
125 const struct drm_display_mode *mode)
127 return nouveau_display_scanoutpos_head(crtc, vpos, hpos,
131 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
132 .destroy = drm_gem_fb_destroy,
133 .create_handle = drm_gem_fb_create_handle,
137 nouveau_decode_mod(struct nouveau_drm *drm,
142 BUG_ON(!tile_mode || !kind);
144 if (modifier == DRM_FORMAT_MOD_LINEAR) {
145 /* tile_mode will not be used in this case */
150 * Extract the block height and kind from the corresponding
151 * modifier fields. See drm_fourcc.h for details.
153 *tile_mode = (uint32_t)(modifier & 0xF);
154 *kind = (uint8_t)((modifier >> 12) & 0xFF);
156 if (drm->client.device.info.chipset >= 0xc0)
162 nouveau_framebuffer_get_layout(struct drm_framebuffer *fb,
166 if (fb->flags & DRM_MODE_FB_MODIFIERS) {
167 struct nouveau_drm *drm = nouveau_drm(fb->dev);
169 nouveau_decode_mod(drm, fb->modifier, tile_mode, kind);
171 const struct nouveau_bo *nvbo = nouveau_gem_object(fb->obj[0]);
173 *tile_mode = nvbo->mode;
179 nouveau_validate_decode_mod(struct nouveau_drm *drm,
184 struct nouveau_display *disp = nouveau_display(drm->dev);
187 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
191 BUG_ON(!disp->format_modifiers);
194 (disp->format_modifiers[mod] != DRM_FORMAT_MOD_INVALID) &&
195 (disp->format_modifiers[mod] != modifier);
198 if (disp->format_modifiers[mod] == DRM_FORMAT_MOD_INVALID)
201 nouveau_decode_mod(drm, modifier, tile_mode, kind);
206 static inline uint32_t
207 nouveau_get_width_in_blocks(uint32_t stride)
209 /* GOBs per block in the x direction is always one, and GOBs are
212 static const uint32_t log_block_width = 6;
214 return (stride + (1 << log_block_width) - 1) >> log_block_width;
217 static inline uint32_t
218 nouveau_get_height_in_blocks(struct nouveau_drm *drm,
220 uint32_t log_block_height_in_gobs)
222 uint32_t log_gob_height;
223 uint32_t log_block_height;
225 BUG_ON(drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA);
227 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI)
232 log_block_height = log_block_height_in_gobs + log_gob_height;
234 return (height + (1 << log_block_height) - 1) >> log_block_height;
238 nouveau_check_bl_size(struct nouveau_drm *drm, struct nouveau_bo *nvbo,
239 uint32_t offset, uint32_t stride, uint32_t h,
242 uint32_t gob_size, bw, bh;
245 BUG_ON(drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA);
247 if (drm->client.device.info.chipset >= 0xc0) {
253 if (tile_mode & 0xFFFFFFF0)
256 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI)
261 bw = nouveau_get_width_in_blocks(stride);
262 bh = nouveau_get_height_in_blocks(drm, h, tile_mode);
264 bl_size = bw * bh * (1 << tile_mode) * gob_size;
266 DRM_DEBUG_KMS("offset=%u stride=%u h=%u tile_mode=0x%02x bw=%u bh=%u gob_size=%u bl_size=%llu size=%lu\n",
267 offset, stride, h, tile_mode, bw, bh, gob_size, bl_size,
270 if (bl_size + offset > nvbo->bo.mem.size)
277 nouveau_framebuffer_new(struct drm_device *dev,
278 const struct drm_mode_fb_cmd2 *mode_cmd,
279 struct drm_gem_object *gem,
280 struct drm_framebuffer **pfb)
282 struct nouveau_drm *drm = nouveau_drm(dev);
283 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
284 struct drm_framebuffer *fb;
285 const struct drm_format_info *info;
286 unsigned int width, height, i;
291 /* YUV overlays have special requirements pre-NV50 */
292 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA &&
294 (mode_cmd->pixel_format == DRM_FORMAT_YUYV ||
295 mode_cmd->pixel_format == DRM_FORMAT_UYVY ||
296 mode_cmd->pixel_format == DRM_FORMAT_NV12 ||
297 mode_cmd->pixel_format == DRM_FORMAT_NV21) &&
298 (mode_cmd->pitches[0] & 0x3f || /* align 64 */
299 mode_cmd->pitches[0] >= 0x10000 || /* at most 64k pitch */
300 (mode_cmd->pitches[1] && /* pitches for planes must match */
301 mode_cmd->pitches[0] != mode_cmd->pitches[1]))) {
302 struct drm_format_name_buf format_name;
303 DRM_DEBUG_KMS("Unsuitable framebuffer: format: %s; pitches: 0x%x\n 0x%x\n",
304 drm_get_format_name(mode_cmd->pixel_format,
306 mode_cmd->pitches[0],
307 mode_cmd->pitches[1]);
311 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
312 if (nouveau_validate_decode_mod(drm, mode_cmd->modifier[0],
313 &tile_mode, &kind)) {
314 DRM_DEBUG_KMS("Unsupported modifier: 0x%llx\n",
315 mode_cmd->modifier[0]);
319 tile_mode = nvbo->mode;
323 info = drm_get_format_info(dev, mode_cmd);
325 for (i = 0; i < info->num_planes; i++) {
326 width = drm_format_info_plane_width(info,
329 height = drm_format_info_plane_height(info,
334 ret = nouveau_check_bl_size(drm, nvbo,
335 mode_cmd->offsets[i],
336 mode_cmd->pitches[i],
341 uint32_t size = mode_cmd->pitches[i] * height;
343 if (size + mode_cmd->offsets[i] > nvbo->bo.mem.size)
348 if (!(fb = *pfb = kzalloc(sizeof(*fb), GFP_KERNEL)))
351 drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
354 ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
360 struct drm_framebuffer *
361 nouveau_user_framebuffer_create(struct drm_device *dev,
362 struct drm_file *file_priv,
363 const struct drm_mode_fb_cmd2 *mode_cmd)
365 struct drm_framebuffer *fb;
366 struct drm_gem_object *gem;
369 gem = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
371 return ERR_PTR(-ENOENT);
373 ret = nouveau_framebuffer_new(dev, mode_cmd, gem, &fb);
377 drm_gem_object_put(gem);
381 static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
382 .fb_create = nouveau_user_framebuffer_create,
383 .output_poll_changed = nouveau_fbcon_output_poll_changed,
387 struct nouveau_drm_prop_enum_list {
393 static struct nouveau_drm_prop_enum_list underscan[] = {
394 { 6, UNDERSCAN_AUTO, "auto" },
395 { 6, UNDERSCAN_OFF, "off" },
396 { 6, UNDERSCAN_ON, "on" },
400 static struct nouveau_drm_prop_enum_list dither_mode[] = {
401 { 7, DITHERING_MODE_AUTO, "auto" },
402 { 7, DITHERING_MODE_OFF, "off" },
403 { 1, DITHERING_MODE_ON, "on" },
404 { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
405 { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
406 { 4, DITHERING_MODE_TEMPORAL, "temporal" },
410 static struct nouveau_drm_prop_enum_list dither_depth[] = {
411 { 6, DITHERING_DEPTH_AUTO, "auto" },
412 { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
413 { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
417 #define PROP_ENUM(p,gen,n,list) do { \
418 struct nouveau_drm_prop_enum_list *l = (list); \
420 while (l->gen_mask) { \
421 if (l->gen_mask & (1 << (gen))) \
426 p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
428 while (p && l->gen_mask) { \
429 if (l->gen_mask & (1 << (gen))) { \
430 drm_property_add_enum(p, l->type, l->name); \
438 nouveau_display_hpd_work(struct work_struct *work)
440 struct nouveau_drm *drm = container_of(work, typeof(*drm), hpd_work);
442 pm_runtime_get_sync(drm->dev->dev);
444 drm_helper_hpd_irq_event(drm->dev);
446 pm_runtime_mark_last_busy(drm->dev->dev);
447 pm_runtime_put_sync(drm->dev->dev);
453 nouveau_display_acpi_ntfy(struct notifier_block *nb, unsigned long val,
456 struct nouveau_drm *drm = container_of(nb, typeof(*drm), acpi_nb);
457 struct acpi_bus_event *info = data;
460 if (!strcmp(info->device_class, ACPI_VIDEO_CLASS)) {
461 if (info->type == ACPI_VIDEO_NOTIFY_PROBE) {
462 ret = pm_runtime_get(drm->dev->dev);
463 if (ret == 1 || ret == -EACCES) {
464 /* If the GPU is already awake, or in a state
465 * where we can't wake it up, it can handle
466 * it's own hotplug events.
468 pm_runtime_put_autosuspend(drm->dev->dev);
469 } else if (ret == 0) {
470 /* This may be the only indication we receive
471 * of a connector hotplug on a runtime
472 * suspended GPU, schedule hpd_work to check.
474 NV_DEBUG(drm, "ACPI requested connector reprobe\n");
475 schedule_work(&drm->hpd_work);
476 pm_runtime_put_noidle(drm->dev->dev);
478 NV_WARN(drm, "Dropped ACPI reprobe event due to RPM error: %d\n",
482 /* acpi-video should not generate keypresses for this */
492 nouveau_display_init(struct drm_device *dev, bool resume, bool runtime)
494 struct nouveau_display *disp = nouveau_display(dev);
495 struct drm_connector *connector;
496 struct drm_connector_list_iter conn_iter;
500 * Enable hotplug interrupts (done as early as possible, since we need
503 drm_connector_list_iter_begin(dev, &conn_iter);
504 nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
505 struct nouveau_connector *conn = nouveau_connector(connector);
506 nvif_notify_get(&conn->hpd);
508 drm_connector_list_iter_end(&conn_iter);
510 ret = disp->init(dev, resume, runtime);
514 /* enable connector detection and polling for connectors without HPD
517 drm_kms_helper_poll_enable(dev);
523 nouveau_display_fini(struct drm_device *dev, bool suspend, bool runtime)
525 struct nouveau_display *disp = nouveau_display(dev);
526 struct nouveau_drm *drm = nouveau_drm(dev);
527 struct drm_connector *connector;
528 struct drm_connector_list_iter conn_iter;
531 if (drm_drv_uses_atomic_modeset(dev))
532 drm_atomic_helper_shutdown(dev);
534 drm_helper_force_disable_all(dev);
537 /* disable hotplug interrupts */
538 drm_connector_list_iter_begin(dev, &conn_iter);
539 nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
540 struct nouveau_connector *conn = nouveau_connector(connector);
541 nvif_notify_put(&conn->hpd);
543 drm_connector_list_iter_end(&conn_iter);
546 cancel_work_sync(&drm->hpd_work);
548 drm_kms_helper_poll_disable(dev);
549 disp->fini(dev, suspend);
553 nouveau_display_create_properties(struct drm_device *dev)
555 struct nouveau_display *disp = nouveau_display(dev);
558 if (disp->disp.object.oclass < NV50_DISP)
561 if (disp->disp.object.oclass < GF110_DISP)
566 PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
567 PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
568 PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
570 disp->underscan_hborder_property =
571 drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
573 disp->underscan_vborder_property =
574 drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
580 disp->vibrant_hue_property =
581 drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
584 disp->color_vibrance_property =
585 drm_property_create_range(dev, 0, "color vibrance", 0, 200);
589 nouveau_display_create(struct drm_device *dev)
591 struct nouveau_drm *drm = nouveau_drm(dev);
592 struct nvkm_device *device = nvxx_device(&drm->client.device);
593 struct nouveau_display *disp;
596 disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
600 drm_mode_config_init(dev);
601 drm_mode_create_scaling_mode_property(dev);
602 drm_mode_create_dvi_i_properties(dev);
604 dev->mode_config.funcs = &nouveau_mode_config_funcs;
605 dev->mode_config.fb_base = device->func->resource_addr(device, 1);
607 dev->mode_config.min_width = 0;
608 dev->mode_config.min_height = 0;
609 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_CELSIUS) {
610 dev->mode_config.max_width = 2048;
611 dev->mode_config.max_height = 2048;
613 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
614 dev->mode_config.max_width = 4096;
615 dev->mode_config.max_height = 4096;
617 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI) {
618 dev->mode_config.max_width = 8192;
619 dev->mode_config.max_height = 8192;
621 dev->mode_config.max_width = 16384;
622 dev->mode_config.max_height = 16384;
625 dev->mode_config.preferred_depth = 24;
626 dev->mode_config.prefer_shadow = 1;
627 dev->mode_config.allow_fb_modifiers = true;
629 if (drm->client.device.info.chipset < 0x11)
630 dev->mode_config.async_page_flip = false;
632 dev->mode_config.async_page_flip = true;
634 drm_kms_helper_poll_init(dev);
635 drm_kms_helper_poll_disable(dev);
637 if (nouveau_modeset != 2 && drm->vbios.dcb.entries) {
638 ret = nvif_disp_ctor(&drm->client.device, "kmsDisp", 0,
641 nouveau_display_create_properties(dev);
642 if (disp->disp.object.oclass < NV50_DISP)
643 ret = nv04_display_create(dev);
645 ret = nv50_display_create(dev);
652 goto disp_create_err;
654 drm_mode_config_reset(dev);
656 if (dev->mode_config.num_crtc) {
657 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
661 if (disp->disp.object.oclass >= NV50_DISP)
665 INIT_WORK(&drm->hpd_work, nouveau_display_hpd_work);
667 drm->acpi_nb.notifier_call = nouveau_display_acpi_ntfy;
668 register_acpi_notifier(&drm->acpi_nb);
676 drm_kms_helper_poll_fini(dev);
677 drm_mode_config_cleanup(dev);
682 nouveau_display_destroy(struct drm_device *dev)
684 struct nouveau_display *disp = nouveau_display(dev);
687 unregister_acpi_notifier(&nouveau_drm(dev)->acpi_nb);
690 drm_kms_helper_poll_fini(dev);
691 drm_mode_config_cleanup(dev);
696 nvif_disp_dtor(&disp->disp);
698 nouveau_drm(dev)->display = NULL;
703 nouveau_display_suspend(struct drm_device *dev, bool runtime)
705 struct nouveau_display *disp = nouveau_display(dev);
707 if (drm_drv_uses_atomic_modeset(dev)) {
709 disp->suspend = drm_atomic_helper_suspend(dev);
710 if (IS_ERR(disp->suspend)) {
711 int ret = PTR_ERR(disp->suspend);
712 disp->suspend = NULL;
718 nouveau_display_fini(dev, true, runtime);
723 nouveau_display_resume(struct drm_device *dev, bool runtime)
725 struct nouveau_display *disp = nouveau_display(dev);
727 nouveau_display_init(dev, true, runtime);
729 if (drm_drv_uses_atomic_modeset(dev)) {
731 drm_atomic_helper_resume(dev, disp->suspend);
732 disp->suspend = NULL;
739 nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
740 struct drm_mode_create_dumb *args)
742 struct nouveau_cli *cli = nouveau_cli(file_priv);
743 struct nouveau_bo *bo;
747 args->pitch = roundup(args->width * (args->bpp / 8), 256);
748 args->size = args->pitch * args->height;
749 args->size = roundup(args->size, PAGE_SIZE);
751 /* Use VRAM if there is any ; otherwise fallback to system memory */
752 if (nouveau_drm(dev)->client.device.info.ram_size != 0)
753 domain = NOUVEAU_GEM_DOMAIN_VRAM;
755 domain = NOUVEAU_GEM_DOMAIN_GART;
757 ret = nouveau_gem_new(cli, args->size, 0, domain, 0, 0, &bo);
761 ret = drm_gem_handle_create(file_priv, &bo->bo.base, &args->handle);
762 drm_gem_object_put(&bo->bo.base);
767 nouveau_display_dumb_map_offset(struct drm_file *file_priv,
768 struct drm_device *dev,
769 uint32_t handle, uint64_t *poffset)
771 struct drm_gem_object *gem;
773 gem = drm_gem_object_lookup(file_priv, handle);
775 struct nouveau_bo *bo = nouveau_gem_object(gem);
776 *poffset = drm_vma_node_offset_addr(&bo->bo.base.vma_node);
777 drm_gem_object_put(gem);