blob: e2488559cc9fd8be46c42d2904b4da091426ede7 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +020028
Thomas Zimmermann2ef79412019-12-03 11:04:02 +010029#include <linux/pci.h>
Dave Airlie10ebc0b2012-09-17 14:40:31 +100030#include <linux/pm_runtime.h>
Sam Ravnborgf9183122019-06-08 10:02:40 +020031#include <linux/slab.h>
32#include <linux/uaccess.h>
33#include <linux/vga_switcheroo.h>
34
35#include <drm/drm_fb_helper.h>
36#include <drm/drm_file.h>
37#include <drm/drm_ioctl.h>
Sam Ravnborgf9183122019-06-08 10:02:40 +020038#include <drm/radeon_drm.h>
39
40#include "radeon.h"
41#include "radeon_asic.h"
Lee Jones9c69e422020-11-06 21:49:37 +000042#include "radeon_drv.h"
Lee Jones59718db2020-11-16 17:29:25 +000043#include "radeon_kms.h"
Alex Deucher78488652014-03-11 15:02:30 -040044
45#if defined(CONFIG_VGA_SWITCHEROO)
Alex Deucher90c4cde2014-04-10 22:29:01 -040046bool radeon_has_atpx(void);
Alex Deucher78488652014-03-11 15:02:30 -040047#else
Alex Deucher90c4cde2014-04-10 22:29:01 -040048static inline bool radeon_has_atpx(void) { return false; }
Alex Deucher78488652014-03-11 15:02:30 -040049#endif
50
Alex Deucherf482a142012-07-17 14:02:34 -040051/**
52 * radeon_driver_unload_kms - Main unload function for KMS.
53 *
54 * @dev: drm dev pointer
55 *
56 * This is the main unload function for KMS (all asics).
57 * It calls radeon_modeset_fini() to tear down the
58 * displays, and radeon_device_fini() to tear down
59 * the rest of the device (CP, writeback, etc.).
60 * Returns 0 on success.
61 */
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -020062void radeon_driver_unload_kms(struct drm_device *dev)
Jerome Glissecf0fe452009-12-09 18:21:55 +010063{
64 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020065
Jerome Glissecf0fe452009-12-09 18:21:55 +010066 if (rdev == NULL)
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -020067 return;
Dave Airlie10ebc0b2012-09-17 14:40:31 +100068
Alex Deucher0cd9cb72013-04-12 19:15:52 -040069 if (rdev->rmmio == NULL)
70 goto done_free;
Dave Airlie10ebc0b2012-09-17 14:40:31 +100071
Lukas Wunner19de6592016-06-08 18:47:27 +020072 if (radeon_is_px(dev)) {
73 pm_runtime_get_sync(dev->dev);
Lukas Wunner8fecb6a2016-06-08 18:47:27 +020074 pm_runtime_forbid(dev->dev);
Lukas Wunner19de6592016-06-08 18:47:27 +020075 }
Dave Airlie10ebc0b2012-09-17 14:40:31 +100076
Alex Deucherc4917072012-07-31 17:14:35 -040077 radeon_acpi_fini(rdev);
Thomas Zimmermannabe39102020-12-01 11:35:37 +010078
Jerome Glissecf0fe452009-12-09 18:21:55 +010079 radeon_modeset_fini(rdev);
80 radeon_device_fini(rdev);
Alex Deucher0cd9cb72013-04-12 19:15:52 -040081
Thomas Zimmermann43359782021-05-07 20:57:07 +020082 if (rdev->agp)
83 arch_phys_wc_del(rdev->agp->agp_mtrr);
84 kfree(rdev->agp);
85 rdev->agp = NULL;
Daniel Vetterb8076b52020-02-22 18:54:32 +010086
Alex Deucher0cd9cb72013-04-12 19:15:52 -040087done_free:
Jerome Glissecf0fe452009-12-09 18:21:55 +010088 kfree(rdev);
89 dev->dev_private = NULL;
Jerome Glissecf0fe452009-12-09 18:21:55 +010090}
91
Alex Deucherf482a142012-07-17 14:02:34 -040092/**
93 * radeon_driver_load_kms - Main load function for KMS.
94 *
95 * @dev: drm dev pointer
96 * @flags: device flags
97 *
98 * This is the main load function for KMS (all asics).
99 * It calls radeon_device_init() to set up the non-display
100 * parts of the chip (asic init, CP, writeback, etc.), and
101 * radeon_modeset_init() to set up the display parts
102 * (crtcs, encoders, hotplug detect, etc.).
103 * Returns 0 on success, error on failure.
104 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200105int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags)
106{
Thomas Zimmermannd86a4122020-12-01 11:35:38 +0100107 struct pci_dev *pdev = to_pci_dev(dev->dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200108 struct radeon_device *rdev;
Alberto Miloned7a29522010-07-06 11:40:24 -0400109 int r, acpi_status;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200110
111 rdev = kzalloc(sizeof(struct radeon_device), GFP_KERNEL);
112 if (rdev == NULL) {
113 return -ENOMEM;
114 }
115 dev->dev_private = (void *)rdev;
116
Thomas Zimmermann5c1736c2021-01-12 09:10:34 +0100117#ifdef __alpha__
118 rdev->hose = pdev->sysdata;
119#endif
120
Thomas Zimmermann43359782021-05-07 20:57:07 +0200121 if (pci_find_capability(pdev, PCI_CAP_ID_AGP))
Nirmoy Das93def702021-09-13 10:08:23 +0200122 rdev->agp = radeon_agp_head_init(dev);
Thomas Zimmermann43359782021-05-07 20:57:07 +0200123 if (rdev->agp) {
124 rdev->agp->agp_mtrr = arch_phys_wc_add(
125 rdev->agp->agp_info.aper_base,
126 rdev->agp->agp_info.aper_size *
127 1024 * 1024);
128 }
129
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200130 /* update BUS flag */
Thomas Zimmermannd86a4122020-12-01 11:35:38 +0100131 if (pci_find_capability(pdev, PCI_CAP_ID_AGP)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200132 flags |= RADEON_IS_AGP;
Thomas Zimmermannd86a4122020-12-01 11:35:38 +0100133 } else if (pci_is_pcie(pdev)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200134 flags |= RADEON_IS_PCIE;
135 } else {
136 flags |= RADEON_IS_PCI;
137 }
138
Alex Deucher73acacc2014-04-15 12:44:35 -0400139 if ((radeon_runtime_pm != 0) &&
140 radeon_has_atpx() &&
Lukas Wunner7ffb0ce32017-03-10 21:23:45 +0100141 ((flags & RADEON_IS_IGP) == 0) &&
Thomas Zimmermannd86a4122020-12-01 11:35:38 +0100142 !pci_is_thunderbolt_attached(pdev))
Alex Deucher90c4cde2014-04-10 22:29:01 -0400143 flags |= RADEON_IS_PX;
144
Jerome Glisse6cf8a3f52009-09-10 21:46:48 +0200145 /* radeon_device_init should report only fatal error
146 * like memory allocation failure or iomapping failure,
147 * or memory manager initialization failure, it must
148 * properly initialize the GPU MC controller and permit
149 * VRAM allocation
150 */
Thomas Zimmermannd86a4122020-12-01 11:35:38 +0100151 r = radeon_device_init(rdev, dev, pdev, flags);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200152 if (r) {
Thomas Zimmermannd86a4122020-12-01 11:35:38 +0100153 dev_err(dev->dev, "Fatal error during GPU init\n");
Jerome Glissecf0fe452009-12-09 18:21:55 +0100154 goto out;
Jerome Glisse6cf8a3f52009-09-10 21:46:48 +0200155 }
Alberto Miloned7a29522010-07-06 11:40:24 -0400156
Jerome Glisse6cf8a3f52009-09-10 21:46:48 +0200157 /* Again modeset_init should fail only on fatal error
158 * otherwise it should provide enough functionalities
159 * for shadowfb to run
160 */
161 r = radeon_modeset_init(rdev);
Jerome Glissecf0fe452009-12-09 18:21:55 +0100162 if (r)
Thomas Zimmermannd86a4122020-12-01 11:35:38 +0100163 dev_err(dev->dev, "Fatal error during modeset init\n");
Luca Tettamantifda4b252012-07-30 21:20:35 +0200164
165 /* Call ACPI methods: require modeset init
166 * but failure is not fatal
167 */
168 if (!r) {
169 acpi_status = radeon_acpi_init(rdev);
170 if (acpi_status)
Xu Wanga6506cd82021-11-10 09:42:42 +0000171 dev_dbg(dev->dev, "Error during ACPI methods call\n");
Luca Tettamantifda4b252012-07-30 21:20:35 +0200172 }
173
Alex Deucher90c4cde2014-04-10 22:29:01 -0400174 if (radeon_is_px(dev)) {
Rafael J. Wysockie0751552020-04-18 18:53:01 +0200175 dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NO_DIRECT_COMPLETE);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000176 pm_runtime_use_autosuspend(dev->dev);
177 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
178 pm_runtime_set_active(dev->dev);
179 pm_runtime_allow(dev->dev);
180 pm_runtime_mark_last_busy(dev->dev);
181 pm_runtime_put_autosuspend(dev->dev);
182 }
183
Jerome Glissecf0fe452009-12-09 18:21:55 +0100184out:
185 if (r)
186 radeon_driver_unload_kms(dev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000187
188
Jerome Glissecf0fe452009-12-09 18:21:55 +0100189 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200190}
191
Alex Deucherf482a142012-07-17 14:02:34 -0400192/**
193 * radeon_set_filp_rights - Set filp right.
194 *
195 * @dev: drm dev pointer
196 * @owner: drm file
197 * @applier: drm file
198 * @value: value
199 *
200 * Sets the filp rights for the device (all asics).
201 */
Marek Olšák9eba4a92011-01-05 05:46:48 +0100202static void radeon_set_filp_rights(struct drm_device *dev,
203 struct drm_file **owner,
204 struct drm_file *applier,
205 uint32_t *value)
206{
Daniel Vetter45c1da52015-10-15 09:36:34 +0200207 struct radeon_device *rdev = dev->dev_private;
208
209 mutex_lock(&rdev->gem.mutex);
Marek Olšák9eba4a92011-01-05 05:46:48 +0100210 if (*value == 1) {
211 /* wants rights */
212 if (!*owner)
213 *owner = applier;
214 } else if (*value == 0) {
215 /* revokes rights */
216 if (*owner == applier)
217 *owner = NULL;
218 }
219 *value = *owner == applier ? 1 : 0;
Daniel Vetter45c1da52015-10-15 09:36:34 +0200220 mutex_unlock(&rdev->gem.mutex);
Marek Olšák9eba4a92011-01-05 05:46:48 +0100221}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200222
223/*
Marek Olšák9eba4a92011-01-05 05:46:48 +0100224 * Userspace get information ioctl
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200225 */
Alex Deucherf482a142012-07-17 14:02:34 -0400226/**
227 * radeon_info_ioctl - answer a device specific request.
228 *
Lee Jonesf5194f72020-11-16 17:29:26 +0000229 * @dev: drm device pointer
Alex Deucherf482a142012-07-17 14:02:34 -0400230 * @data: request object
231 * @filp: drm filp
232 *
233 * This function is used to pass device specific parameters to the userspace
234 * drivers. Examples include: pci device id, pipeline parms, tiling params,
235 * etc. (all asics).
236 * Returns 0 on success, -EINVAL on failure.
237 */
Daniel Vetter384bc5e2020-11-04 11:04:20 +0100238int radeon_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200239{
240 struct radeon_device *rdev = dev->dev_private;
Marek Olšák6759a0a2012-08-09 16:34:17 +0200241 struct drm_radeon_info *info = data;
Jerome Glissebc35afd2010-05-12 18:01:13 +0200242 struct radeon_mode_info *minfo = &rdev->mode_info;
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400243 uint32_t *value, value_tmp, *value_ptr, value_size;
244 uint64_t value64;
Jerome Glissebc35afd2010-05-12 18:01:13 +0200245 struct drm_crtc *crtc;
246 int i, found;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200247
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200248 value_ptr = (uint32_t *)((unsigned long)info->value);
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400249 value = &value_tmp;
250 value_size = sizeof(uint32_t);
Dr. David Alan Gilbertd8ab3552010-08-02 09:43:52 +1000251
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200252 switch (info->request) {
253 case RADEON_INFO_DEVICE_ID:
Thomas Zimmermannd86a4122020-12-01 11:35:38 +0100254 *value = to_pci_dev(dev->dev)->device;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200255 break;
256 case RADEON_INFO_NUM_GB_PIPES:
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400257 *value = rdev->num_gb_pipes;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200258 break;
Alex Deucherf779b3e2009-08-19 19:11:39 -0400259 case RADEON_INFO_NUM_Z_PIPES:
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400260 *value = rdev->num_z_pipes;
Alex Deucherf779b3e2009-08-19 19:11:39 -0400261 break;
Jerome Glisse733289c2009-09-16 15:24:21 +0200262 case RADEON_INFO_ACCEL_WORKING:
Alex Deucher148a03b2010-06-03 19:00:03 -0400263 /* xf86-video-ati 6.13.0 relies on this being false for evergreen */
264 if ((rdev->family >= CHIP_CEDAR) && (rdev->family <= CHIP_HEMLOCK))
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400265 *value = false;
Alex Deucher148a03b2010-06-03 19:00:03 -0400266 else
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400267 *value = rdev->accel_working;
Jerome Glisse733289c2009-09-16 15:24:21 +0200268 break;
Jerome Glissebc35afd2010-05-12 18:01:13 +0200269 case RADEON_INFO_CRTC_FROM_ID:
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100270 if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400271 DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
272 return -EFAULT;
273 }
Jerome Glissebc35afd2010-05-12 18:01:13 +0200274 for (i = 0, found = 0; i < rdev->num_crtc; i++) {
275 crtc = (struct drm_crtc *)minfo->crtcs[i];
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400276 if (crtc && crtc->base.id == *value) {
Alex Deucher0baf2d82010-07-21 14:05:35 -0400277 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400278 *value = radeon_crtc->crtc_id;
Jerome Glissebc35afd2010-05-12 18:01:13 +0200279 found = 1;
280 break;
281 }
282 }
283 if (!found) {
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400284 DRM_DEBUG_KMS("unknown crtc id %d\n", *value);
Jerome Glissebc35afd2010-05-12 18:01:13 +0200285 return -EINVAL;
286 }
287 break;
Alex Deucher148a03b2010-06-03 19:00:03 -0400288 case RADEON_INFO_ACCEL_WORKING2:
Alex Deucher3c64bd22014-08-01 20:05:30 +0200289 if (rdev->family == CHIP_HAWAII) {
Andreas Boll9eb401a2014-08-01 20:05:32 +0200290 if (rdev->accel_working) {
291 if (rdev->new_fw)
292 *value = 3;
293 else
294 *value = 2;
295 } else {
Alex Deucher3c64bd22014-08-01 20:05:30 +0200296 *value = 0;
Andreas Boll9eb401a2014-08-01 20:05:32 +0200297 }
Alex Deucher3c64bd22014-08-01 20:05:30 +0200298 } else {
299 *value = rdev->accel_working;
300 }
Alex Deucher148a03b2010-06-03 19:00:03 -0400301 break;
Alex Deuchere7aeeba62010-06-04 13:10:12 -0400302 case RADEON_INFO_TILING_CONFIG:
Alex Deucher64f759c2012-07-06 17:40:32 -0400303 if (rdev->family >= CHIP_BONAIRE)
304 *value = rdev->config.cik.tile_config;
305 else if (rdev->family >= CHIP_TAHITI)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400306 *value = rdev->config.si.tile_config;
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400307 else if (rdev->family >= CHIP_CAYMAN)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400308 *value = rdev->config.cayman.tile_config;
Alex Deucherfecf1d02011-03-02 20:07:29 -0500309 else if (rdev->family >= CHIP_CEDAR)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400310 *value = rdev->config.evergreen.tile_config;
Alex Deuchere7aeeba62010-06-04 13:10:12 -0400311 else if (rdev->family >= CHIP_RV770)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400312 *value = rdev->config.rv770.tile_config;
Alex Deuchere7aeeba62010-06-04 13:10:12 -0400313 else if (rdev->family >= CHIP_R600)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400314 *value = rdev->config.r600.tile_config;
Alex Deuchere7aeeba62010-06-04 13:10:12 -0400315 else {
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000316 DRM_DEBUG_KMS("tiling config is r6xx+ only!\n");
Alex Deuchere7aeeba62010-06-04 13:10:12 -0400317 return -EINVAL;
318 }
Alex Deucherb824b362010-08-12 08:25:47 -0400319 break;
Dave Airlieab9e1f52010-07-13 11:11:11 +1000320 case RADEON_INFO_WANT_HYPERZ:
Marek Olšák43861f72010-08-07 03:36:34 +0200321 /* The "value" here is both an input and output parameter.
322 * If the input value is 1, filp requests hyper-z access.
323 * If the input value is 0, filp revokes its hyper-z access.
324 *
325 * When returning, the value is 1 if filp owns hyper-z access,
326 * 0 otherwise. */
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100327 if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400328 DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
329 return -EFAULT;
330 }
331 if (*value >= 2) {
332 DRM_DEBUG_KMS("WANT_HYPERZ: invalid value %d\n", *value);
Marek Olšák43861f72010-08-07 03:36:34 +0200333 return -EINVAL;
Dave Airlieab9e1f52010-07-13 11:11:11 +1000334 }
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400335 radeon_set_filp_rights(dev, &rdev->hyperz_filp, filp, value);
Marek Olšák9eba4a92011-01-05 05:46:48 +0100336 break;
337 case RADEON_INFO_WANT_CMASK:
338 /* The same logic as Hyper-Z. */
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100339 if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400340 DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
341 return -EFAULT;
342 }
343 if (*value >= 2) {
344 DRM_DEBUG_KMS("WANT_CMASK: invalid value %d\n", *value);
Marek Olšák9eba4a92011-01-05 05:46:48 +0100345 return -EINVAL;
Marek Olšák43861f72010-08-07 03:36:34 +0200346 }
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400347 radeon_set_filp_rights(dev, &rdev->cmask_filp, filp, value);
Alex Deuchere7aeeba62010-06-04 13:10:12 -0400348 break;
Alex Deucher58bbf012011-01-24 17:14:26 -0500349 case RADEON_INFO_CLOCK_CRYSTAL_FREQ:
350 /* return clock value in KHz */
Alex Deucher454d2e22013-02-14 10:04:02 -0500351 if (rdev->asic->get_xclk)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400352 *value = radeon_get_xclk(rdev) * 10;
Alex Deucher454d2e22013-02-14 10:04:02 -0500353 else
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400354 *value = rdev->clock.spll.reference_freq * 10;
Alex Deucher58bbf012011-01-24 17:14:26 -0500355 break;
Dave Airlie486af182011-03-01 14:32:27 +1000356 case RADEON_INFO_NUM_BACKENDS:
Alex Deucher64f759c2012-07-06 17:40:32 -0400357 if (rdev->family >= CHIP_BONAIRE)
358 *value = rdev->config.cik.max_backends_per_se *
359 rdev->config.cik.max_shader_engines;
360 else if (rdev->family >= CHIP_TAHITI)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400361 *value = rdev->config.si.max_backends_per_se *
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400362 rdev->config.si.max_shader_engines;
363 else if (rdev->family >= CHIP_CAYMAN)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400364 *value = rdev->config.cayman.max_backends_per_se *
Alex Deucherfecf1d02011-03-02 20:07:29 -0500365 rdev->config.cayman.max_shader_engines;
366 else if (rdev->family >= CHIP_CEDAR)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400367 *value = rdev->config.evergreen.max_backends;
Dave Airlie486af182011-03-01 14:32:27 +1000368 else if (rdev->family >= CHIP_RV770)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400369 *value = rdev->config.rv770.max_backends;
Dave Airlie486af182011-03-01 14:32:27 +1000370 else if (rdev->family >= CHIP_R600)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400371 *value = rdev->config.r600.max_backends;
Dave Airlie486af182011-03-01 14:32:27 +1000372 else {
373 return -EINVAL;
374 }
375 break;
Alex Deucher65659452011-04-26 13:27:43 -0400376 case RADEON_INFO_NUM_TILE_PIPES:
Alex Deucher64f759c2012-07-06 17:40:32 -0400377 if (rdev->family >= CHIP_BONAIRE)
378 *value = rdev->config.cik.max_tile_pipes;
379 else if (rdev->family >= CHIP_TAHITI)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400380 *value = rdev->config.si.max_tile_pipes;
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400381 else if (rdev->family >= CHIP_CAYMAN)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400382 *value = rdev->config.cayman.max_tile_pipes;
Alex Deucher65659452011-04-26 13:27:43 -0400383 else if (rdev->family >= CHIP_CEDAR)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400384 *value = rdev->config.evergreen.max_tile_pipes;
Alex Deucher65659452011-04-26 13:27:43 -0400385 else if (rdev->family >= CHIP_RV770)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400386 *value = rdev->config.rv770.max_tile_pipes;
Alex Deucher65659452011-04-26 13:27:43 -0400387 else if (rdev->family >= CHIP_R600)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400388 *value = rdev->config.r600.max_tile_pipes;
Alex Deucher65659452011-04-26 13:27:43 -0400389 else {
390 return -EINVAL;
391 }
392 break;
Alex Deucher8aeb96f82011-05-03 19:28:02 -0400393 case RADEON_INFO_FUSION_GART_WORKING:
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400394 *value = 1;
Alex Deucher8aeb96f82011-05-03 19:28:02 -0400395 break;
Alex Deuchere55b9422011-07-15 19:53:52 +0000396 case RADEON_INFO_BACKEND_MAP:
Alex Deucher64f759c2012-07-06 17:40:32 -0400397 if (rdev->family >= CHIP_BONAIRE)
Michel Dänzer1ddce272013-11-18 18:25:59 +0900398 *value = rdev->config.cik.backend_map;
Alex Deucher64f759c2012-07-06 17:40:32 -0400399 else if (rdev->family >= CHIP_TAHITI)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400400 *value = rdev->config.si.backend_map;
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400401 else if (rdev->family >= CHIP_CAYMAN)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400402 *value = rdev->config.cayman.backend_map;
Alex Deuchere55b9422011-07-15 19:53:52 +0000403 else if (rdev->family >= CHIP_CEDAR)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400404 *value = rdev->config.evergreen.backend_map;
Alex Deuchere55b9422011-07-15 19:53:52 +0000405 else if (rdev->family >= CHIP_RV770)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400406 *value = rdev->config.rv770.backend_map;
Alex Deuchere55b9422011-07-15 19:53:52 +0000407 else if (rdev->family >= CHIP_R600)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400408 *value = rdev->config.r600.backend_map;
Alex Deuchere55b9422011-07-15 19:53:52 +0000409 else {
410 return -EINVAL;
411 }
412 break;
Jerome Glisse721604a2012-01-05 22:11:05 -0500413 case RADEON_INFO_VA_START:
414 /* this is where we report if vm is supported or not */
415 if (rdev->family < CHIP_CAYMAN)
416 return -EINVAL;
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400417 *value = RADEON_VA_RESERVED_SIZE;
Jerome Glisse721604a2012-01-05 22:11:05 -0500418 break;
419 case RADEON_INFO_IB_VM_MAX_SIZE:
420 /* this is where we report if vm is supported or not */
421 if (rdev->family < CHIP_CAYMAN)
422 return -EINVAL;
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400423 *value = RADEON_IB_VM_MAX_SIZE;
Jerome Glisse721604a2012-01-05 22:11:05 -0500424 break;
Tom Stellard609c1e12012-03-20 17:17:55 -0400425 case RADEON_INFO_MAX_PIPES:
Alex Deucher64f759c2012-07-06 17:40:32 -0400426 if (rdev->family >= CHIP_BONAIRE)
427 *value = rdev->config.cik.max_cu_per_sh;
428 else if (rdev->family >= CHIP_TAHITI)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400429 *value = rdev->config.si.max_cu_per_sh;
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400430 else if (rdev->family >= CHIP_CAYMAN)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400431 *value = rdev->config.cayman.max_pipes_per_simd;
Tom Stellard609c1e12012-03-20 17:17:55 -0400432 else if (rdev->family >= CHIP_CEDAR)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400433 *value = rdev->config.evergreen.max_pipes;
Tom Stellard609c1e12012-03-20 17:17:55 -0400434 else if (rdev->family >= CHIP_RV770)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400435 *value = rdev->config.rv770.max_pipes;
Tom Stellard609c1e12012-03-20 17:17:55 -0400436 else if (rdev->family >= CHIP_R600)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400437 *value = rdev->config.r600.max_pipes;
Tom Stellard609c1e12012-03-20 17:17:55 -0400438 else {
439 return -EINVAL;
440 }
441 break;
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400442 case RADEON_INFO_TIMESTAMP:
443 if (rdev->family < CHIP_R600) {
444 DRM_DEBUG_KMS("timestamp is r6xx+ only!\n");
445 return -EINVAL;
446 }
447 value = (uint32_t*)&value64;
448 value_size = sizeof(uint64_t);
449 value64 = radeon_get_gpu_clock_counter(rdev);
450 break;
Alex Deucher2e1a7672012-12-04 12:55:37 -0500451 case RADEON_INFO_MAX_SE:
Alex Deucher64f759c2012-07-06 17:40:32 -0400452 if (rdev->family >= CHIP_BONAIRE)
453 *value = rdev->config.cik.max_shader_engines;
454 else if (rdev->family >= CHIP_TAHITI)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400455 *value = rdev->config.si.max_shader_engines;
Alex Deucher2e1a7672012-12-04 12:55:37 -0500456 else if (rdev->family >= CHIP_CAYMAN)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400457 *value = rdev->config.cayman.max_shader_engines;
Alex Deucher2e1a7672012-12-04 12:55:37 -0500458 else if (rdev->family >= CHIP_CEDAR)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400459 *value = rdev->config.evergreen.num_ses;
Alex Deucher2e1a7672012-12-04 12:55:37 -0500460 else
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400461 *value = 1;
Alex Deucher2e1a7672012-12-04 12:55:37 -0500462 break;
463 case RADEON_INFO_MAX_SH_PER_SE:
Alex Deucher64f759c2012-07-06 17:40:32 -0400464 if (rdev->family >= CHIP_BONAIRE)
465 *value = rdev->config.cik.max_sh_per_se;
466 else if (rdev->family >= CHIP_TAHITI)
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400467 *value = rdev->config.si.max_sh_per_se;
Alex Deucher2e1a7672012-12-04 12:55:37 -0500468 else
469 return -EINVAL;
470 break;
Samuel Lia0a53aa2013-04-08 17:25:47 -0400471 case RADEON_INFO_FASTFB_WORKING:
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400472 *value = rdev->fastfb_working;
Samuel Lia0a53aa2013-04-08 17:25:47 -0400473 break;
Christian König902aaef2013-04-09 10:35:42 -0400474 case RADEON_INFO_RING_WORKING:
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100475 if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400476 DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
477 return -EFAULT;
478 }
479 switch (*value) {
Christian König902aaef2013-04-09 10:35:42 -0400480 case RADEON_CS_RING_GFX:
481 case RADEON_CS_RING_COMPUTE:
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400482 *value = rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready;
Christian König902aaef2013-04-09 10:35:42 -0400483 break;
484 case RADEON_CS_RING_DMA:
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400485 *value = rdev->ring[R600_RING_TYPE_DMA_INDEX].ready;
486 *value |= rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready;
Christian König902aaef2013-04-09 10:35:42 -0400487 break;
488 case RADEON_CS_RING_UVD:
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400489 *value = rdev->ring[R600_RING_TYPE_UVD_INDEX].ready;
Christian König902aaef2013-04-09 10:35:42 -0400490 break;
Christian Königf7ba8b02014-01-27 10:16:06 -0700491 case RADEON_CS_RING_VCE:
492 *value = rdev->ring[TN_RING_TYPE_VCE1_INDEX].ready;
493 break;
Christian König902aaef2013-04-09 10:35:42 -0400494 default:
495 return -EINVAL;
496 }
497 break;
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400498 case RADEON_INFO_SI_TILE_MODE_ARRAY:
Alex Deucher64f759c2012-07-06 17:40:32 -0400499 if (rdev->family >= CHIP_BONAIRE) {
Alex Deucher39aee492013-04-10 13:41:25 -0400500 value = rdev->config.cik.tile_mode_array;
501 value_size = sizeof(uint32_t)*32;
502 } else if (rdev->family >= CHIP_TAHITI) {
503 value = rdev->config.si.tile_mode_array;
504 value_size = sizeof(uint32_t)*32;
505 } else {
506 DRM_DEBUG_KMS("tile mode array is si+ only!\n");
Alex Deucher64f759c2012-07-06 17:40:32 -0400507 return -EINVAL;
508 }
Jerome Glisse64d7b8b2013-04-09 11:17:08 -0400509 break;
Michel Dänzer32f79a82013-11-18 18:26:00 +0900510 case RADEON_INFO_CIK_MACROTILE_MODE_ARRAY:
511 if (rdev->family >= CHIP_BONAIRE) {
512 value = rdev->config.cik.macrotile_mode_array;
513 value_size = sizeof(uint32_t)*16;
514 } else {
515 DRM_DEBUG_KMS("macrotile mode array is cik+ only!\n");
516 return -EINVAL;
517 }
518 break;
Tom Stellarde5b9e752013-08-16 17:47:39 -0400519 case RADEON_INFO_SI_CP_DMA_COMPUTE:
520 *value = 1;
521 break;
Marek Olšák439a1cf2013-12-22 02:18:01 +0100522 case RADEON_INFO_SI_BACKEND_ENABLED_MASK:
523 if (rdev->family >= CHIP_BONAIRE) {
524 *value = rdev->config.cik.backend_enable_mask;
525 } else if (rdev->family >= CHIP_TAHITI) {
526 *value = rdev->config.si.backend_enable_mask;
527 } else {
528 DRM_DEBUG_KMS("BACKEND_ENABLED_MASK is si+ only!\n");
Colin Ian King8dbc2cc2021-03-03 00:27:59 +0000529 return -EINVAL;
Marek Olšák439a1cf2013-12-22 02:18:01 +0100530 }
531 break;
Alex Deucherf5f1f892014-01-20 18:20:29 -0500532 case RADEON_INFO_MAX_SCLK:
533 if ((rdev->pm.pm_method == PM_METHOD_DPM) &&
534 rdev->pm.dpm_enabled)
535 *value = rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk * 10;
536 else
537 *value = rdev->pm.default_sclk * 10;
538 break;
Christian König98ccc292014-01-23 09:50:49 -0700539 case RADEON_INFO_VCE_FW_VERSION:
540 *value = rdev->vce.fw_version;
541 break;
542 case RADEON_INFO_VCE_FB_VERSION:
543 *value = rdev->vce.fb_version;
544 break;
Marek Olšák67e8e3f2014-03-02 00:56:18 +0100545 case RADEON_INFO_NUM_BYTES_MOVED:
546 value = (uint32_t*)&value64;
547 value_size = sizeof(uint64_t);
548 value64 = atomic64_read(&rdev->num_bytes_moved);
549 break;
550 case RADEON_INFO_VRAM_USAGE:
551 value = (uint32_t*)&value64;
552 value_size = sizeof(uint64_t);
553 value64 = atomic64_read(&rdev->vram_usage);
554 break;
555 case RADEON_INFO_GTT_USAGE:
556 value = (uint32_t*)&value64;
557 value_size = sizeof(uint64_t);
558 value64 = atomic64_read(&rdev->gtt_usage);
559 break;
Alex Deucher65fcf662014-06-02 16:13:21 -0400560 case RADEON_INFO_ACTIVE_CU_COUNT:
561 if (rdev->family >= CHIP_BONAIRE)
562 *value = rdev->config.cik.active_cus;
563 else if (rdev->family >= CHIP_TAHITI)
564 *value = rdev->config.si.active_cus;
565 else if (rdev->family >= CHIP_CAYMAN)
566 *value = rdev->config.cayman.active_simds;
567 else if (rdev->family >= CHIP_CEDAR)
568 *value = rdev->config.evergreen.active_simds;
569 else if (rdev->family >= CHIP_RV770)
570 *value = rdev->config.rv770.active_simds;
571 else if (rdev->family >= CHIP_R600)
572 *value = rdev->config.r600.active_simds;
573 else
574 *value = 1;
575 break;
Alex Deucherd6d2a182014-09-30 10:04:40 -0400576 case RADEON_INFO_CURRENT_GPU_TEMP:
577 /* get temperature in millidegrees C */
578 if (rdev->asic->pm.get_temperature)
579 *value = radeon_get_temperature(rdev);
580 else
581 *value = 0;
582 break;
Alex Deucher5c363a82014-09-30 11:33:30 -0400583 case RADEON_INFO_CURRENT_GPU_SCLK:
584 /* get sclk in Mhz */
585 if (rdev->pm.dpm_enabled)
586 *value = radeon_dpm_get_current_sclk(rdev) / 100;
587 else
588 *value = rdev->pm.current_sclk / 100;
589 break;
590 case RADEON_INFO_CURRENT_GPU_MCLK:
591 /* get mclk in Mhz */
592 if (rdev->pm.dpm_enabled)
593 *value = radeon_dpm_get_current_mclk(rdev) / 100;
594 else
595 *value = rdev->pm.current_mclk / 100;
596 break;
Alex Deucher4535cb92014-10-01 11:26:50 -0400597 case RADEON_INFO_READ_REG:
598 if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
599 DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
600 return -EFAULT;
601 }
602 if (radeon_get_allowed_info_register(rdev, *value, value))
603 return -EINVAL;
604 break;
Michel Dänzer3bc980b2015-06-16 17:28:16 +0900605 case RADEON_INFO_VA_UNMAP_WORKING:
606 *value = true;
607 break;
Marek Olšák72b90762015-04-29 19:40:33 +0200608 case RADEON_INFO_GPU_RESET_COUNTER:
609 *value = atomic_read(&rdev->gpu_reset_counter);
610 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200611 default:
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000612 DRM_DEBUG_KMS("Invalid request %d\n", info->request);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200613 return -EINVAL;
614 }
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100615 if (copy_to_user(value_ptr, (char*)value, value_size)) {
Marek Olšák6759a0a2012-08-09 16:34:17 +0200616 DRM_ERROR("copy_to_user %s:%u\n", __func__, __LINE__);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200617 return -EFAULT;
618 }
619 return 0;
620}
621
622
623/*
624 * Outdated mess for old drm with Xorg being in charge (void function now).
625 */
Alex Deucherf482a142012-07-17 14:02:34 -0400626/**
Alex Deucher8c70e1c2015-10-02 16:52:58 -0400627 * radeon_driver_lastclose_kms - drm callback for last close
Alex Deucherf482a142012-07-17 14:02:34 -0400628 *
629 * @dev: drm dev pointer
630 *
Lukas Wunner8e5de1d2015-09-05 11:14:43 +0200631 * Switch vga_switcheroo state after last close (all asics).
Alex Deucherf482a142012-07-17 14:02:34 -0400632 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200633void radeon_driver_lastclose_kms(struct drm_device *dev)
634{
Noralf Trønnes3997eea2017-12-05 19:25:02 +0100635 drm_fb_helper_lastclose(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000636 vga_switcheroo_process_delayed_switch();
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200637}
638
Alex Deucherf482a142012-07-17 14:02:34 -0400639/**
640 * radeon_driver_open_kms - drm callback for open
641 *
642 * @dev: drm dev pointer
643 * @file_priv: drm file
644 *
645 * On device open, init vm on cayman+ (all asics).
646 * Returns 0 on success, error on failure.
647 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200648int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
649{
Jerome Glisse721604a2012-01-05 22:11:05 -0500650 struct radeon_device *rdev = dev->dev_private;
Zhou Qingyangab50cb92021-12-01 23:13:10 +0800651 struct radeon_fpriv *fpriv;
652 struct radeon_vm *vm;
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000653 int r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500654
655 file_priv->driver_priv = NULL;
656
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000657 r = pm_runtime_get_sync(dev->dev);
Aditya Pakki9fb10672020-06-13 21:21:22 -0500658 if (r < 0) {
659 pm_runtime_put_autosuspend(dev->dev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000660 return r;
Aditya Pakki9fb10672020-06-13 21:21:22 -0500661 }
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000662
Jerome Glisse721604a2012-01-05 22:11:05 -0500663 /* new gpu have virtual address space support */
664 if (rdev->family >= CHIP_CAYMAN) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500665
666 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
667 if (unlikely(!fpriv)) {
Alex Deucher32c59dc2016-08-31 17:27:03 -0400668 r = -ENOMEM;
669 goto out_suspend;
Jerome Glisse721604a2012-01-05 22:11:05 -0500670 }
671
Jérôme Glisse24f47ac2014-05-07 16:35:24 -0400672 if (rdev->accel_working) {
Alex Deucher544143f2015-01-28 14:36:26 -0500673 vm = &fpriv->vm;
674 r = radeon_vm_init(rdev, vm);
Zhou Qingyangab50cb92021-12-01 23:13:10 +0800675 if (r)
676 goto out_fpriv;
Alex Deucher544143f2015-01-28 14:36:26 -0500677
Jérôme Glisse24f47ac2014-05-07 16:35:24 -0400678 r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
Zhou Qingyangab50cb92021-12-01 23:13:10 +0800679 if (r)
680 goto out_vm_fini;
Jérôme Glisse24f47ac2014-05-07 16:35:24 -0400681
682 /* map the ib pool buffer read only into
683 * virtual address space */
Christian Königcc9e67e2014-07-18 13:48:10 +0200684 vm->ib_bo_va = radeon_vm_bo_add(rdev, vm,
685 rdev->ring_tmp_bo.bo);
Zhou Qingyangab50cb92021-12-01 23:13:10 +0800686 if (!vm->ib_bo_va) {
687 r = -ENOMEM;
688 goto out_vm_fini;
689 }
690
Christian Königcc9e67e2014-07-18 13:48:10 +0200691 r = radeon_vm_bo_set_addr(rdev, vm->ib_bo_va,
692 RADEON_VA_IB_OFFSET,
Jérôme Glisse24f47ac2014-05-07 16:35:24 -0400693 RADEON_VM_PAGE_READABLE |
694 RADEON_VM_PAGE_SNOOPED);
Zhou Qingyangab50cb92021-12-01 23:13:10 +0800695 if (r)
696 goto out_vm_fini;
Quentin Casasnovas74073c92014-03-18 17:16:52 +0100697 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500698 file_priv->driver_priv = fpriv;
699 }
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000700
Zhou Qingyangab50cb92021-12-01 23:13:10 +0800701 if (!r)
702 goto out_suspend;
703
704out_vm_fini:
705 radeon_vm_fini(rdev, vm);
706out_fpriv:
707 kfree(fpriv);
Alex Deucher32c59dc2016-08-31 17:27:03 -0400708out_suspend:
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000709 pm_runtime_mark_last_busy(dev->dev);
710 pm_runtime_put_autosuspend(dev->dev);
Alex Deucher32c59dc2016-08-31 17:27:03 -0400711 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200712}
713
Alex Deucherf482a142012-07-17 14:02:34 -0400714/**
715 * radeon_driver_postclose_kms - drm callback for post close
716 *
717 * @dev: drm dev pointer
718 * @file_priv: drm file
719 *
Daniel Vetter78910242017-03-08 15:12:48 +0100720 * On device close, tear down hyperz and cmask filps on r1xx-r5xx
721 * (all asics). And tear down vm on cayman+ (all asics).
Alex Deucherf482a142012-07-17 14:02:34 -0400722 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200723void radeon_driver_postclose_kms(struct drm_device *dev,
724 struct drm_file *file_priv)
725{
Jerome Glisse721604a2012-01-05 22:11:05 -0500726 struct radeon_device *rdev = dev->dev_private;
727
Daniel Vetter78910242017-03-08 15:12:48 +0100728 pm_runtime_get_sync(dev->dev);
729
730 mutex_lock(&rdev->gem.mutex);
731 if (rdev->hyperz_filp == file_priv)
732 rdev->hyperz_filp = NULL;
733 if (rdev->cmask_filp == file_priv)
734 rdev->cmask_filp = NULL;
735 mutex_unlock(&rdev->gem.mutex);
736
737 radeon_uvd_free_handles(rdev, file_priv);
738 radeon_vce_free_handles(rdev, file_priv);
739
Jerome Glisse721604a2012-01-05 22:11:05 -0500740 /* new gpu have virtual address space support */
741 if (rdev->family >= CHIP_CAYMAN && file_priv->driver_priv) {
742 struct radeon_fpriv *fpriv = file_priv->driver_priv;
Christian Königcc9e67e2014-07-18 13:48:10 +0200743 struct radeon_vm *vm = &fpriv->vm;
Christian Königd72d43c2012-10-09 13:31:18 +0200744 int r;
745
Jérôme Glisse24f47ac2014-05-07 16:35:24 -0400746 if (rdev->accel_working) {
747 r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
748 if (!r) {
Christian Königcc9e67e2014-07-18 13:48:10 +0200749 if (vm->ib_bo_va)
750 radeon_vm_bo_rmv(rdev, vm->ib_bo_va);
Jérôme Glisse24f47ac2014-05-07 16:35:24 -0400751 radeon_bo_unreserve(rdev->ring_tmp_bo.bo);
752 }
Alex Deucher544143f2015-01-28 14:36:26 -0500753 radeon_vm_fini(rdev, vm);
Christian Königd72d43c2012-10-09 13:31:18 +0200754 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500755
Jerome Glisse721604a2012-01-05 22:11:05 -0500756 kfree(fpriv);
757 file_priv->driver_priv = NULL;
758 }
Alex Deucher9b96b632016-08-31 17:23:31 -0400759 pm_runtime_mark_last_busy(dev->dev);
760 pm_runtime_put_autosuspend(dev->dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200761}
762
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200763/*
764 * VBlank related functions.
765 */
Alex Deucherf482a142012-07-17 14:02:34 -0400766/**
767 * radeon_get_vblank_counter_kms - get frame count
768 *
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100769 * @crtc: crtc to get the frame count from
Alex Deucherf482a142012-07-17 14:02:34 -0400770 *
771 * Gets the frame count on the requested crtc (all asics).
772 * Returns frame count on success, -EINVAL on failure.
773 */
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100774u32 radeon_get_vblank_counter_kms(struct drm_crtc *crtc)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200775{
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100776 struct drm_device *dev = crtc->dev;
777 unsigned int pipe = crtc->index;
Mario Kleinerc55d21e2015-11-25 20:14:31 +0100778 int vpos, hpos, stat;
779 u32 count;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200780 struct radeon_device *rdev = dev->dev_private;
781
Thierry Reding85a21ea2016-01-04 18:19:12 +0100782 if (pipe >= rdev->num_crtc) {
Thierry Reding4e926d22015-12-16 15:31:47 +0100783 DRM_ERROR("Invalid crtc %u\n", pipe);
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200784 return -EINVAL;
785 }
786
Mario Kleinerc55d21e2015-11-25 20:14:31 +0100787 /* The hw increments its frame counter at start of vsync, not at start
788 * of vblank, as is required by DRM core vblank counter handling.
789 * Cook the hw count here to make it appear to the caller as if it
790 * incremented at start of vblank. We measure distance to start of
791 * vblank in vpos. vpos therefore will be >= 0 between start of vblank
792 * and start of vsync, so vpos >= 0 means to bump the hw frame counter
793 * result by 1 to give the proper appearance to caller.
794 */
Thierry Reding4e926d22015-12-16 15:31:47 +0100795 if (rdev->mode_info.crtcs[pipe]) {
Mario Kleinerc55d21e2015-11-25 20:14:31 +0100796 /* Repeat readout if needed to provide stable result if
797 * we cross start of vsync during the queries.
798 */
799 do {
Thierry Reding4e926d22015-12-16 15:31:47 +0100800 count = radeon_get_vblank_counter(rdev, pipe);
Mario Kleinerc55d21e2015-11-25 20:14:31 +0100801 /* Ask radeon_get_crtc_scanoutpos to return vpos as
802 * distance to start of vblank, instead of regular
803 * vertical scanout pos.
804 */
805 stat = radeon_get_crtc_scanoutpos(
Thierry Reding4e926d22015-12-16 15:31:47 +0100806 dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
Mario Kleinerc55d21e2015-11-25 20:14:31 +0100807 &vpos, &hpos, NULL, NULL,
Thierry Reding4e926d22015-12-16 15:31:47 +0100808 &rdev->mode_info.crtcs[pipe]->base.hwmode);
809 } while (count != radeon_get_vblank_counter(rdev, pipe));
Mario Kleinerc55d21e2015-11-25 20:14:31 +0100810
811 if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
812 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
813 DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
814 }
815 else {
Thierry Reding4e926d22015-12-16 15:31:47 +0100816 DRM_DEBUG_VBL("crtc %u: dist from vblank start %d\n",
817 pipe, vpos);
Mario Kleinerc55d21e2015-11-25 20:14:31 +0100818
819 /* Bump counter if we are at >= leading edge of vblank,
820 * but before vsync where vpos would turn negative and
821 * the hw counter really increments.
822 */
823 if (vpos >= 0)
824 count++;
825 }
826 }
827 else {
828 /* Fallback to use value as is. */
Thierry Reding4e926d22015-12-16 15:31:47 +0100829 count = radeon_get_vblank_counter(rdev, pipe);
Mario Kleinerc55d21e2015-11-25 20:14:31 +0100830 DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
831 }
832
833 return count;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200834}
835
Alex Deucherf482a142012-07-17 14:02:34 -0400836/**
837 * radeon_enable_vblank_kms - enable vblank interrupt
838 *
Alex Deucherf482a142012-07-17 14:02:34 -0400839 * @crtc: crtc to enable vblank interrupt for
840 *
841 * Enable the interrupt on the requested crtc (all asics).
842 * Returns 0 on success, -EINVAL on failure.
843 */
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100844int radeon_enable_vblank_kms(struct drm_crtc *crtc)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200845{
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100846 struct drm_device *dev = crtc->dev;
847 unsigned int pipe = crtc->index;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200848 struct radeon_device *rdev = dev->dev_private;
Christian Koenigfb982572012-05-17 01:33:30 +0200849 unsigned long irqflags;
850 int r;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200851
ChenTaoa2eb03a2020-05-05 12:57:37 +0800852 if (pipe >= rdev->num_crtc) {
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100853 DRM_ERROR("Invalid crtc %d\n", pipe);
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200854 return -EINVAL;
855 }
856
Christian Koenigfb982572012-05-17 01:33:30 +0200857 spin_lock_irqsave(&rdev->irq.lock, irqflags);
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100858 rdev->irq.crtc_vblank_int[pipe] = true;
Christian Koenigfb982572012-05-17 01:33:30 +0200859 r = radeon_irq_set(rdev);
860 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
861 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200862}
863
Alex Deucherf482a142012-07-17 14:02:34 -0400864/**
865 * radeon_disable_vblank_kms - disable vblank interrupt
866 *
Alex Deucherf482a142012-07-17 14:02:34 -0400867 * @crtc: crtc to disable vblank interrupt for
868 *
869 * Disable the interrupt on the requested crtc (all asics).
870 */
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100871void radeon_disable_vblank_kms(struct drm_crtc *crtc)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200872{
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100873 struct drm_device *dev = crtc->dev;
874 unsigned int pipe = crtc->index;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200875 struct radeon_device *rdev = dev->dev_private;
Christian Koenigfb982572012-05-17 01:33:30 +0200876 unsigned long irqflags;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200877
ChenTaoa2eb03a2020-05-05 12:57:37 +0800878 if (pipe >= rdev->num_crtc) {
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100879 DRM_ERROR("Invalid crtc %d\n", pipe);
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200880 return;
881 }
882
Christian Koenigfb982572012-05-17 01:33:30 +0200883 spin_lock_irqsave(&rdev->irq.lock, irqflags);
Thomas Zimmermannd7757ba2020-01-23 14:59:32 +0100884 rdev->irq.crtc_vblank_int[pipe] = false;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200885 radeon_irq_set(rdev);
Christian Koenigfb982572012-05-17 01:33:30 +0200886 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200887}