1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright (C) 2013-2017 Oracle Corporation 4 * This file is based on ast_main.c 5 * Copyright 2012 Red Hat Inc. 6 * Authors: Dave Airlie <airlied@redhat.com>, 7 * Michael Thayer <michael.thayer@oracle.com, 8 * Hans de Goede <hdegoede@redhat.com> 9 */ 10 11 #include <linux/pci.h> 12 #include <linux/vbox_err.h> 13 14 #include <drm/drm_damage_helper.h> 15 16 #include "vbox_drv.h" 17 #include "vboxvideo_guest.h" 18 #include "vboxvideo_vbe.h" 19 20 void vbox_report_caps(struct vbox_private *vbox) 21 { 22 u32 caps = VBVACAPS_DISABLE_CURSOR_INTEGRATION | 23 VBVACAPS_IRQ | VBVACAPS_USE_VBVA_ONLY; 24 25 /* The host only accepts VIDEO_MODE_HINTS if it is send separately. */ 26 hgsmi_send_caps_info(vbox->guest_pool, caps); 27 caps |= VBVACAPS_VIDEO_MODE_HINTS; 28 hgsmi_send_caps_info(vbox->guest_pool, caps); 29 } 30 31 static int vbox_accel_init(struct vbox_private *vbox) 32 { 33 struct pci_dev *pdev = to_pci_dev(vbox->ddev.dev); 34 struct vbva_buffer *vbva; 35 unsigned int i; 36 37 vbox->vbva_info = devm_kcalloc(vbox->ddev.dev, vbox->num_crtcs, 38 sizeof(*vbox->vbva_info), GFP_KERNEL); 39 if (!vbox->vbva_info) 40 return -ENOMEM; 41 42 /* Take a command buffer for each screen from the end of usable VRAM. */ 43 vbox->available_vram_size -= vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE; 44 45 vbox->vbva_buffers = pcim_iomap_range( 46 pdev, 0, vbox->available_vram_size, 47 vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE); 48 if (IS_ERR(vbox->vbva_buffers)) 49 return PTR_ERR(vbox->vbva_buffers); 50 51 for (i = 0; i < vbox->num_crtcs; ++i) { 52 vbva_setup_buffer_context(&vbox->vbva_info[i], 53 vbox->available_vram_size + 54 i * VBVA_MIN_BUFFER_SIZE, 55 VBVA_MIN_BUFFER_SIZE); 56 vbva = (void __force *)vbox->vbva_buffers + 57 i * VBVA_MIN_BUFFER_SIZE; 58 if (!vbva_enable(&vbox->vbva_info[i], 59 vbox->guest_pool, vbva, i)) { 60 /* very old host or driver error. */ 61 DRM_ERROR("vboxvideo: vbva_enable failed\n"); 62 } 63 } 64 65 return 0; 66 } 67 68 static void vbox_accel_fini(struct vbox_private *vbox) 69 { 70 unsigned int i; 71 72 for (i = 0; i < vbox->num_crtcs; ++i) 73 vbva_disable(&vbox->vbva_info[i], vbox->guest_pool, i); 74 } 75 76 /* Do we support the 4.3 plus mode hint reporting interface? */ 77 static bool have_hgsmi_mode_hints(struct vbox_private *vbox) 78 { 79 u32 have_hints, have_cursor; 80 int ret; 81 82 ret = hgsmi_query_conf(vbox->guest_pool, 83 VBOX_VBVA_CONF32_MODE_HINT_REPORTING, 84 &have_hints); 85 if (ret) 86 return false; 87 88 ret = hgsmi_query_conf(vbox->guest_pool, 89 VBOX_VBVA_CONF32_GUEST_CURSOR_REPORTING, 90 &have_cursor); 91 if (ret) 92 return false; 93 94 return have_hints == VINF_SUCCESS && have_cursor == VINF_SUCCESS; 95 } 96 97 bool vbox_check_supported(u16 id) 98 { 99 u16 dispi_id; 100 101 vbox_write_ioport(VBE_DISPI_INDEX_ID, id); 102 dispi_id = inw(VBE_DISPI_IOPORT_DATA); 103 104 return dispi_id == id; 105 } 106 107 int vbox_hw_init(struct vbox_private *vbox) 108 { 109 struct pci_dev *pdev = to_pci_dev(vbox->ddev.dev); 110 int ret = -ENOMEM; 111 112 vbox->full_vram_size = inl(VBE_DISPI_IOPORT_DATA); 113 vbox->any_pitch = vbox_check_supported(VBE_DISPI_ID_ANYX); 114 115 DRM_INFO("VRAM %08x\n", vbox->full_vram_size); 116 117 ret = pcim_request_region(pdev, 0, "vboxvideo"); 118 if (ret) 119 return ret; 120 121 /* Map guest-heap at end of vram */ 122 vbox->guest_heap = pcim_iomap_range(pdev, 0, 123 GUEST_HEAP_OFFSET(vbox), GUEST_HEAP_SIZE); 124 if (IS_ERR(vbox->guest_heap)) 125 return PTR_ERR(vbox->guest_heap); 126 127 /* Create guest-heap mem-pool use 2^4 = 16 byte chunks */ 128 vbox->guest_pool = devm_gen_pool_create(vbox->ddev.dev, 4, -1, 129 "vboxvideo-accel"); 130 if (IS_ERR(vbox->guest_pool)) 131 return PTR_ERR(vbox->guest_pool); 132 133 ret = gen_pool_add_virt(vbox->guest_pool, 134 (unsigned long)vbox->guest_heap, 135 GUEST_HEAP_OFFSET(vbox), 136 GUEST_HEAP_USABLE_SIZE, -1); 137 if (ret) 138 return ret; 139 140 ret = hgsmi_test_query_conf(vbox->guest_pool); 141 if (ret) { 142 DRM_ERROR("vboxvideo: hgsmi_test_query_conf failed\n"); 143 return ret; 144 } 145 146 /* Reduce available VRAM size to reflect the guest heap. */ 147 vbox->available_vram_size = GUEST_HEAP_OFFSET(vbox); 148 /* Linux drm represents monitors as a 32-bit array. */ 149 hgsmi_query_conf(vbox->guest_pool, VBOX_VBVA_CONF32_MONITOR_COUNT, 150 &vbox->num_crtcs); 151 vbox->num_crtcs = clamp_t(u32, vbox->num_crtcs, 1, VBOX_MAX_SCREENS); 152 153 if (!have_hgsmi_mode_hints(vbox)) { 154 ret = -ENOTSUPP; 155 return ret; 156 } 157 158 vbox->last_mode_hints = devm_kcalloc(vbox->ddev.dev, vbox->num_crtcs, 159 sizeof(struct vbva_modehint), 160 GFP_KERNEL); 161 if (!vbox->last_mode_hints) 162 return -ENOMEM; 163 164 ret = vbox_accel_init(vbox); 165 if (ret) 166 return ret; 167 168 return 0; 169 } 170 171 void vbox_hw_fini(struct vbox_private *vbox) 172 { 173 vbox_accel_fini(vbox); 174 } 175