1 /*
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  */
28 #include <drm/drmP.h>
29 #include "radeon.h"
30 #include <drm/radeon_drm.h>
31 #include "radeon_asic.h"
32 
33 #include <linux/vga_switcheroo.h>
34 #include <linux/slab.h>
35 
36 /**
37  * radeon_driver_unload_kms - Main unload function for KMS.
38  *
39  * @dev: drm dev pointer
40  *
41  * This is the main unload function for KMS (all asics).
42  * It calls radeon_modeset_fini() to tear down the
43  * displays, and radeon_device_fini() to tear down
44  * the rest of the device (CP, writeback, etc.).
45  * Returns 0 on success.
46  */
47 int radeon_driver_unload_kms(struct drm_device *dev)
48 {
49 	struct radeon_device *rdev = dev->dev_private;
50 
51 	if (rdev == NULL)
52 		return 0;
53 	if (rdev->rmmio == NULL)
54 		goto done_free;
55 	radeon_acpi_fini(rdev);
56 	radeon_modeset_fini(rdev);
57 	radeon_device_fini(rdev);
58 
59 done_free:
60 	kfree(rdev);
61 	dev->dev_private = NULL;
62 	return 0;
63 }
64 
65 /**
66  * radeon_driver_load_kms - Main load function for KMS.
67  *
68  * @dev: drm dev pointer
69  * @flags: device flags
70  *
71  * This is the main load function for KMS (all asics).
72  * It calls radeon_device_init() to set up the non-display
73  * parts of the chip (asic init, CP, writeback, etc.), and
74  * radeon_modeset_init() to set up the display parts
75  * (crtcs, encoders, hotplug detect, etc.).
76  * Returns 0 on success, error on failure.
77  */
78 int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags)
79 {
80 	struct radeon_device *rdev;
81 	int r, acpi_status;
82 
83 	rdev = kzalloc(sizeof(struct radeon_device), GFP_KERNEL);
84 	if (rdev == NULL) {
85 		return -ENOMEM;
86 	}
87 	dev->dev_private = (void *)rdev;
88 
89 	/* update BUS flag */
90 	if (drm_pci_device_is_agp(dev)) {
91 		flags |= RADEON_IS_AGP;
92 	} else if (pci_is_pcie(dev->pdev)) {
93 		flags |= RADEON_IS_PCIE;
94 	} else {
95 		flags |= RADEON_IS_PCI;
96 	}
97 
98 	/* radeon_device_init should report only fatal error
99 	 * like memory allocation failure or iomapping failure,
100 	 * or memory manager initialization failure, it must
101 	 * properly initialize the GPU MC controller and permit
102 	 * VRAM allocation
103 	 */
104 	r = radeon_device_init(rdev, dev, dev->pdev, flags);
105 	if (r) {
106 		dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
107 		goto out;
108 	}
109 
110 	/* Again modeset_init should fail only on fatal error
111 	 * otherwise it should provide enough functionalities
112 	 * for shadowfb to run
113 	 */
114 	r = radeon_modeset_init(rdev);
115 	if (r)
116 		dev_err(&dev->pdev->dev, "Fatal error during modeset init\n");
117 
118 	/* Call ACPI methods: require modeset init
119 	 * but failure is not fatal
120 	 */
121 	if (!r) {
122 		acpi_status = radeon_acpi_init(rdev);
123 		if (acpi_status)
124 		dev_dbg(&dev->pdev->dev,
125 				"Error during ACPI methods call\n");
126 	}
127 
128 out:
129 	if (r)
130 		radeon_driver_unload_kms(dev);
131 	return r;
132 }
133 
134 /**
135  * radeon_set_filp_rights - Set filp right.
136  *
137  * @dev: drm dev pointer
138  * @owner: drm file
139  * @applier: drm file
140  * @value: value
141  *
142  * Sets the filp rights for the device (all asics).
143  */
144 static void radeon_set_filp_rights(struct drm_device *dev,
145 				   struct drm_file **owner,
146 				   struct drm_file *applier,
147 				   uint32_t *value)
148 {
149 	mutex_lock(&dev->struct_mutex);
150 	if (*value == 1) {
151 		/* wants rights */
152 		if (!*owner)
153 			*owner = applier;
154 	} else if (*value == 0) {
155 		/* revokes rights */
156 		if (*owner == applier)
157 			*owner = NULL;
158 	}
159 	*value = *owner == applier ? 1 : 0;
160 	mutex_unlock(&dev->struct_mutex);
161 }
162 
163 /*
164  * Userspace get information ioctl
165  */
166 /**
167  * radeon_info_ioctl - answer a device specific request.
168  *
169  * @rdev: radeon device pointer
170  * @data: request object
171  * @filp: drm filp
172  *
173  * This function is used to pass device specific parameters to the userspace
174  * drivers.  Examples include: pci device id, pipeline parms, tiling params,
175  * etc. (all asics).
176  * Returns 0 on success, -EINVAL on failure.
177  */
178 int radeon_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
179 {
180 	struct radeon_device *rdev = dev->dev_private;
181 	struct drm_radeon_info *info = data;
182 	struct radeon_mode_info *minfo = &rdev->mode_info;
183 	uint32_t *value, value_tmp, *value_ptr, value_size;
184 	uint64_t value64;
185 	struct drm_crtc *crtc;
186 	int i, found;
187 
188 	value_ptr = (uint32_t *)((unsigned long)info->value);
189 	value = &value_tmp;
190 	value_size = sizeof(uint32_t);
191 
192 	switch (info->request) {
193 	case RADEON_INFO_DEVICE_ID:
194 		*value = dev->pci_device;
195 		break;
196 	case RADEON_INFO_NUM_GB_PIPES:
197 		*value = rdev->num_gb_pipes;
198 		break;
199 	case RADEON_INFO_NUM_Z_PIPES:
200 		*value = rdev->num_z_pipes;
201 		break;
202 	case RADEON_INFO_ACCEL_WORKING:
203 		/* xf86-video-ati 6.13.0 relies on this being false for evergreen */
204 		if ((rdev->family >= CHIP_CEDAR) && (rdev->family <= CHIP_HEMLOCK))
205 			*value = false;
206 		else
207 			*value = rdev->accel_working;
208 		break;
209 	case RADEON_INFO_CRTC_FROM_ID:
210 		if (DRM_COPY_FROM_USER(value, value_ptr, sizeof(uint32_t))) {
211 			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
212 			return -EFAULT;
213 		}
214 		for (i = 0, found = 0; i < rdev->num_crtc; i++) {
215 			crtc = (struct drm_crtc *)minfo->crtcs[i];
216 			if (crtc && crtc->base.id == *value) {
217 				struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
218 				*value = radeon_crtc->crtc_id;
219 				found = 1;
220 				break;
221 			}
222 		}
223 		if (!found) {
224 			DRM_DEBUG_KMS("unknown crtc id %d\n", *value);
225 			return -EINVAL;
226 		}
227 		break;
228 	case RADEON_INFO_ACCEL_WORKING2:
229 		*value = rdev->accel_working;
230 		break;
231 	case RADEON_INFO_TILING_CONFIG:
232 		if (rdev->family >= CHIP_BONAIRE)
233 			*value = rdev->config.cik.tile_config;
234 		else if (rdev->family >= CHIP_TAHITI)
235 			*value = rdev->config.si.tile_config;
236 		else if (rdev->family >= CHIP_CAYMAN)
237 			*value = rdev->config.cayman.tile_config;
238 		else if (rdev->family >= CHIP_CEDAR)
239 			*value = rdev->config.evergreen.tile_config;
240 		else if (rdev->family >= CHIP_RV770)
241 			*value = rdev->config.rv770.tile_config;
242 		else if (rdev->family >= CHIP_R600)
243 			*value = rdev->config.r600.tile_config;
244 		else {
245 			DRM_DEBUG_KMS("tiling config is r6xx+ only!\n");
246 			return -EINVAL;
247 		}
248 		break;
249 	case RADEON_INFO_WANT_HYPERZ:
250 		/* The "value" here is both an input and output parameter.
251 		 * If the input value is 1, filp requests hyper-z access.
252 		 * If the input value is 0, filp revokes its hyper-z access.
253 		 *
254 		 * When returning, the value is 1 if filp owns hyper-z access,
255 		 * 0 otherwise. */
256 		if (DRM_COPY_FROM_USER(value, value_ptr, sizeof(uint32_t))) {
257 			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
258 			return -EFAULT;
259 		}
260 		if (*value >= 2) {
261 			DRM_DEBUG_KMS("WANT_HYPERZ: invalid value %d\n", *value);
262 			return -EINVAL;
263 		}
264 		radeon_set_filp_rights(dev, &rdev->hyperz_filp, filp, value);
265 		break;
266 	case RADEON_INFO_WANT_CMASK:
267 		/* The same logic as Hyper-Z. */
268 		if (DRM_COPY_FROM_USER(value, value_ptr, sizeof(uint32_t))) {
269 			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
270 			return -EFAULT;
271 		}
272 		if (*value >= 2) {
273 			DRM_DEBUG_KMS("WANT_CMASK: invalid value %d\n", *value);
274 			return -EINVAL;
275 		}
276 		radeon_set_filp_rights(dev, &rdev->cmask_filp, filp, value);
277 		break;
278 	case RADEON_INFO_CLOCK_CRYSTAL_FREQ:
279 		/* return clock value in KHz */
280 		if (rdev->asic->get_xclk)
281 			*value = radeon_get_xclk(rdev) * 10;
282 		else
283 			*value = rdev->clock.spll.reference_freq * 10;
284 		break;
285 	case RADEON_INFO_NUM_BACKENDS:
286 		if (rdev->family >= CHIP_BONAIRE)
287 			*value = rdev->config.cik.max_backends_per_se *
288 				rdev->config.cik.max_shader_engines;
289 		else if (rdev->family >= CHIP_TAHITI)
290 			*value = rdev->config.si.max_backends_per_se *
291 				rdev->config.si.max_shader_engines;
292 		else if (rdev->family >= CHIP_CAYMAN)
293 			*value = rdev->config.cayman.max_backends_per_se *
294 				rdev->config.cayman.max_shader_engines;
295 		else if (rdev->family >= CHIP_CEDAR)
296 			*value = rdev->config.evergreen.max_backends;
297 		else if (rdev->family >= CHIP_RV770)
298 			*value = rdev->config.rv770.max_backends;
299 		else if (rdev->family >= CHIP_R600)
300 			*value = rdev->config.r600.max_backends;
301 		else {
302 			return -EINVAL;
303 		}
304 		break;
305 	case RADEON_INFO_NUM_TILE_PIPES:
306 		if (rdev->family >= CHIP_BONAIRE)
307 			*value = rdev->config.cik.max_tile_pipes;
308 		else if (rdev->family >= CHIP_TAHITI)
309 			*value = rdev->config.si.max_tile_pipes;
310 		else if (rdev->family >= CHIP_CAYMAN)
311 			*value = rdev->config.cayman.max_tile_pipes;
312 		else if (rdev->family >= CHIP_CEDAR)
313 			*value = rdev->config.evergreen.max_tile_pipes;
314 		else if (rdev->family >= CHIP_RV770)
315 			*value = rdev->config.rv770.max_tile_pipes;
316 		else if (rdev->family >= CHIP_R600)
317 			*value = rdev->config.r600.max_tile_pipes;
318 		else {
319 			return -EINVAL;
320 		}
321 		break;
322 	case RADEON_INFO_FUSION_GART_WORKING:
323 		*value = 1;
324 		break;
325 	case RADEON_INFO_BACKEND_MAP:
326 		if (rdev->family >= CHIP_BONAIRE)
327 			return -EINVAL;
328 		else if (rdev->family >= CHIP_TAHITI)
329 			*value = rdev->config.si.backend_map;
330 		else if (rdev->family >= CHIP_CAYMAN)
331 			*value = rdev->config.cayman.backend_map;
332 		else if (rdev->family >= CHIP_CEDAR)
333 			*value = rdev->config.evergreen.backend_map;
334 		else if (rdev->family >= CHIP_RV770)
335 			*value = rdev->config.rv770.backend_map;
336 		else if (rdev->family >= CHIP_R600)
337 			*value = rdev->config.r600.backend_map;
338 		else {
339 			return -EINVAL;
340 		}
341 		break;
342 	case RADEON_INFO_VA_START:
343 		/* this is where we report if vm is supported or not */
344 		if (rdev->family < CHIP_CAYMAN)
345 			return -EINVAL;
346 		*value = RADEON_VA_RESERVED_SIZE;
347 		break;
348 	case RADEON_INFO_IB_VM_MAX_SIZE:
349 		/* this is where we report if vm is supported or not */
350 		if (rdev->family < CHIP_CAYMAN)
351 			return -EINVAL;
352 		*value = RADEON_IB_VM_MAX_SIZE;
353 		break;
354 	case RADEON_INFO_MAX_PIPES:
355 		if (rdev->family >= CHIP_BONAIRE)
356 			*value = rdev->config.cik.max_cu_per_sh;
357 		else if (rdev->family >= CHIP_TAHITI)
358 			*value = rdev->config.si.max_cu_per_sh;
359 		else if (rdev->family >= CHIP_CAYMAN)
360 			*value = rdev->config.cayman.max_pipes_per_simd;
361 		else if (rdev->family >= CHIP_CEDAR)
362 			*value = rdev->config.evergreen.max_pipes;
363 		else if (rdev->family >= CHIP_RV770)
364 			*value = rdev->config.rv770.max_pipes;
365 		else if (rdev->family >= CHIP_R600)
366 			*value = rdev->config.r600.max_pipes;
367 		else {
368 			return -EINVAL;
369 		}
370 		break;
371 	case RADEON_INFO_TIMESTAMP:
372 		if (rdev->family < CHIP_R600) {
373 			DRM_DEBUG_KMS("timestamp is r6xx+ only!\n");
374 			return -EINVAL;
375 		}
376 		value = (uint32_t*)&value64;
377 		value_size = sizeof(uint64_t);
378 		value64 = radeon_get_gpu_clock_counter(rdev);
379 		break;
380 	case RADEON_INFO_MAX_SE:
381 		if (rdev->family >= CHIP_BONAIRE)
382 			*value = rdev->config.cik.max_shader_engines;
383 		else if (rdev->family >= CHIP_TAHITI)
384 			*value = rdev->config.si.max_shader_engines;
385 		else if (rdev->family >= CHIP_CAYMAN)
386 			*value = rdev->config.cayman.max_shader_engines;
387 		else if (rdev->family >= CHIP_CEDAR)
388 			*value = rdev->config.evergreen.num_ses;
389 		else
390 			*value = 1;
391 		break;
392 	case RADEON_INFO_MAX_SH_PER_SE:
393 		if (rdev->family >= CHIP_BONAIRE)
394 			*value = rdev->config.cik.max_sh_per_se;
395 		else if (rdev->family >= CHIP_TAHITI)
396 			*value = rdev->config.si.max_sh_per_se;
397 		else
398 			return -EINVAL;
399 		break;
400 	case RADEON_INFO_FASTFB_WORKING:
401 		*value = rdev->fastfb_working;
402 		break;
403 	case RADEON_INFO_RING_WORKING:
404 		if (DRM_COPY_FROM_USER(value, value_ptr, sizeof(uint32_t))) {
405 			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
406 			return -EFAULT;
407 		}
408 		switch (*value) {
409 		case RADEON_CS_RING_GFX:
410 		case RADEON_CS_RING_COMPUTE:
411 			*value = rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready;
412 			break;
413 		case RADEON_CS_RING_DMA:
414 			*value = rdev->ring[R600_RING_TYPE_DMA_INDEX].ready;
415 			*value |= rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready;
416 			break;
417 		case RADEON_CS_RING_UVD:
418 			*value = rdev->ring[R600_RING_TYPE_UVD_INDEX].ready;
419 			break;
420 		default:
421 			return -EINVAL;
422 		}
423 		break;
424 	case RADEON_INFO_SI_TILE_MODE_ARRAY:
425 		if (rdev->family >= CHIP_BONAIRE) {
426 			value = rdev->config.cik.tile_mode_array;
427 			value_size = sizeof(uint32_t)*32;
428 		} else if (rdev->family >= CHIP_TAHITI) {
429 			value = rdev->config.si.tile_mode_array;
430 			value_size = sizeof(uint32_t)*32;
431 		} else {
432 			DRM_DEBUG_KMS("tile mode array is si+ only!\n");
433 			return -EINVAL;
434 		}
435 		break;
436 	case RADEON_INFO_SI_CP_DMA_COMPUTE:
437 		*value = 1;
438 		break;
439 	default:
440 		DRM_DEBUG_KMS("Invalid request %d\n", info->request);
441 		return -EINVAL;
442 	}
443 	if (DRM_COPY_TO_USER(value_ptr, (char*)value, value_size)) {
444 		DRM_ERROR("copy_to_user %s:%u\n", __func__, __LINE__);
445 		return -EFAULT;
446 	}
447 	return 0;
448 }
449 
450 
451 /*
452  * Outdated mess for old drm with Xorg being in charge (void function now).
453  */
454 /**
455  * radeon_driver_firstopen_kms - drm callback for last close
456  *
457  * @dev: drm dev pointer
458  *
459  * Switch vga switcheroo state after last close (all asics).
460  */
461 void radeon_driver_lastclose_kms(struct drm_device *dev)
462 {
463 	vga_switcheroo_process_delayed_switch();
464 }
465 
466 /**
467  * radeon_driver_open_kms - drm callback for open
468  *
469  * @dev: drm dev pointer
470  * @file_priv: drm file
471  *
472  * On device open, init vm on cayman+ (all asics).
473  * Returns 0 on success, error on failure.
474  */
475 int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
476 {
477 	struct radeon_device *rdev = dev->dev_private;
478 
479 	file_priv->driver_priv = NULL;
480 
481 	/* new gpu have virtual address space support */
482 	if (rdev->family >= CHIP_CAYMAN) {
483 		struct radeon_fpriv *fpriv;
484 		struct radeon_bo_va *bo_va;
485 		int r;
486 
487 		fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
488 		if (unlikely(!fpriv)) {
489 			return -ENOMEM;
490 		}
491 
492 		radeon_vm_init(rdev, &fpriv->vm);
493 
494 		/* map the ib pool buffer read only into
495 		 * virtual address space */
496 		bo_va = radeon_vm_bo_add(rdev, &fpriv->vm,
497 					 rdev->ring_tmp_bo.bo);
498 		r = radeon_vm_bo_set_addr(rdev, bo_va, RADEON_VA_IB_OFFSET,
499 					  RADEON_VM_PAGE_READABLE |
500 					  RADEON_VM_PAGE_SNOOPED);
501 		if (r) {
502 			radeon_vm_fini(rdev, &fpriv->vm);
503 			kfree(fpriv);
504 			return r;
505 		}
506 
507 		file_priv->driver_priv = fpriv;
508 	}
509 	return 0;
510 }
511 
512 /**
513  * radeon_driver_postclose_kms - drm callback for post close
514  *
515  * @dev: drm dev pointer
516  * @file_priv: drm file
517  *
518  * On device post close, tear down vm on cayman+ (all asics).
519  */
520 void radeon_driver_postclose_kms(struct drm_device *dev,
521 				 struct drm_file *file_priv)
522 {
523 	struct radeon_device *rdev = dev->dev_private;
524 
525 	/* new gpu have virtual address space support */
526 	if (rdev->family >= CHIP_CAYMAN && file_priv->driver_priv) {
527 		struct radeon_fpriv *fpriv = file_priv->driver_priv;
528 		struct radeon_bo_va *bo_va;
529 		int r;
530 
531 		r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
532 		if (!r) {
533 			bo_va = radeon_vm_bo_find(&fpriv->vm,
534 						  rdev->ring_tmp_bo.bo);
535 			if (bo_va)
536 				radeon_vm_bo_rmv(rdev, bo_va);
537 			radeon_bo_unreserve(rdev->ring_tmp_bo.bo);
538 		}
539 
540 		radeon_vm_fini(rdev, &fpriv->vm);
541 		kfree(fpriv);
542 		file_priv->driver_priv = NULL;
543 	}
544 }
545 
546 /**
547  * radeon_driver_preclose_kms - drm callback for pre close
548  *
549  * @dev: drm dev pointer
550  * @file_priv: drm file
551  *
552  * On device pre close, tear down hyperz and cmask filps on r1xx-r5xx
553  * (all asics).
554  */
555 void radeon_driver_preclose_kms(struct drm_device *dev,
556 				struct drm_file *file_priv)
557 {
558 	struct radeon_device *rdev = dev->dev_private;
559 	if (rdev->hyperz_filp == file_priv)
560 		rdev->hyperz_filp = NULL;
561 	if (rdev->cmask_filp == file_priv)
562 		rdev->cmask_filp = NULL;
563 	radeon_uvd_free_handles(rdev, file_priv);
564 }
565 
566 /*
567  * VBlank related functions.
568  */
569 /**
570  * radeon_get_vblank_counter_kms - get frame count
571  *
572  * @dev: drm dev pointer
573  * @crtc: crtc to get the frame count from
574  *
575  * Gets the frame count on the requested crtc (all asics).
576  * Returns frame count on success, -EINVAL on failure.
577  */
578 u32 radeon_get_vblank_counter_kms(struct drm_device *dev, int crtc)
579 {
580 	struct radeon_device *rdev = dev->dev_private;
581 
582 	if (crtc < 0 || crtc >= rdev->num_crtc) {
583 		DRM_ERROR("Invalid crtc %d\n", crtc);
584 		return -EINVAL;
585 	}
586 
587 	return radeon_get_vblank_counter(rdev, crtc);
588 }
589 
590 /**
591  * radeon_enable_vblank_kms - enable vblank interrupt
592  *
593  * @dev: drm dev pointer
594  * @crtc: crtc to enable vblank interrupt for
595  *
596  * Enable the interrupt on the requested crtc (all asics).
597  * Returns 0 on success, -EINVAL on failure.
598  */
599 int radeon_enable_vblank_kms(struct drm_device *dev, int crtc)
600 {
601 	struct radeon_device *rdev = dev->dev_private;
602 	unsigned long irqflags;
603 	int r;
604 
605 	if (crtc < 0 || crtc >= rdev->num_crtc) {
606 		DRM_ERROR("Invalid crtc %d\n", crtc);
607 		return -EINVAL;
608 	}
609 
610 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
611 	rdev->irq.crtc_vblank_int[crtc] = true;
612 	r = radeon_irq_set(rdev);
613 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
614 	return r;
615 }
616 
617 /**
618  * radeon_disable_vblank_kms - disable vblank interrupt
619  *
620  * @dev: drm dev pointer
621  * @crtc: crtc to disable vblank interrupt for
622  *
623  * Disable the interrupt on the requested crtc (all asics).
624  */
625 void radeon_disable_vblank_kms(struct drm_device *dev, int crtc)
626 {
627 	struct radeon_device *rdev = dev->dev_private;
628 	unsigned long irqflags;
629 
630 	if (crtc < 0 || crtc >= rdev->num_crtc) {
631 		DRM_ERROR("Invalid crtc %d\n", crtc);
632 		return;
633 	}
634 
635 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
636 	rdev->irq.crtc_vblank_int[crtc] = false;
637 	radeon_irq_set(rdev);
638 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
639 }
640 
641 /**
642  * radeon_get_vblank_timestamp_kms - get vblank timestamp
643  *
644  * @dev: drm dev pointer
645  * @crtc: crtc to get the timestamp for
646  * @max_error: max error
647  * @vblank_time: time value
648  * @flags: flags passed to the driver
649  *
650  * Gets the timestamp on the requested crtc based on the
651  * scanout position.  (all asics).
652  * Returns postive status flags on success, negative error on failure.
653  */
654 int radeon_get_vblank_timestamp_kms(struct drm_device *dev, int crtc,
655 				    int *max_error,
656 				    struct timeval *vblank_time,
657 				    unsigned flags)
658 {
659 	struct drm_crtc *drmcrtc;
660 	struct radeon_device *rdev = dev->dev_private;
661 
662 	if (crtc < 0 || crtc >= dev->num_crtcs) {
663 		DRM_ERROR("Invalid crtc %d\n", crtc);
664 		return -EINVAL;
665 	}
666 
667 	/* Get associated drm_crtc: */
668 	drmcrtc = &rdev->mode_info.crtcs[crtc]->base;
669 
670 	/* Helper routine in DRM core does all the work: */
671 	return drm_calc_vbltimestamp_from_scanoutpos(dev, crtc, max_error,
672 						     vblank_time, flags,
673 						     drmcrtc);
674 }
675 
676 #define KMS_INVALID_IOCTL(name)						\
677 int name(struct drm_device *dev, void *data, struct drm_file *file_priv)\
678 {									\
679 	DRM_ERROR("invalid ioctl with kms %s\n", __func__);		\
680 	return -EINVAL;							\
681 }
682 
683 /*
684  * All these ioctls are invalid in kms world.
685  */
686 KMS_INVALID_IOCTL(radeon_cp_init_kms)
687 KMS_INVALID_IOCTL(radeon_cp_start_kms)
688 KMS_INVALID_IOCTL(radeon_cp_stop_kms)
689 KMS_INVALID_IOCTL(radeon_cp_reset_kms)
690 KMS_INVALID_IOCTL(radeon_cp_idle_kms)
691 KMS_INVALID_IOCTL(radeon_cp_resume_kms)
692 KMS_INVALID_IOCTL(radeon_engine_reset_kms)
693 KMS_INVALID_IOCTL(radeon_fullscreen_kms)
694 KMS_INVALID_IOCTL(radeon_cp_swap_kms)
695 KMS_INVALID_IOCTL(radeon_cp_clear_kms)
696 KMS_INVALID_IOCTL(radeon_cp_vertex_kms)
697 KMS_INVALID_IOCTL(radeon_cp_indices_kms)
698 KMS_INVALID_IOCTL(radeon_cp_texture_kms)
699 KMS_INVALID_IOCTL(radeon_cp_stipple_kms)
700 KMS_INVALID_IOCTL(radeon_cp_indirect_kms)
701 KMS_INVALID_IOCTL(radeon_cp_vertex2_kms)
702 KMS_INVALID_IOCTL(radeon_cp_cmdbuf_kms)
703 KMS_INVALID_IOCTL(radeon_cp_getparam_kms)
704 KMS_INVALID_IOCTL(radeon_cp_flip_kms)
705 KMS_INVALID_IOCTL(radeon_mem_alloc_kms)
706 KMS_INVALID_IOCTL(radeon_mem_free_kms)
707 KMS_INVALID_IOCTL(radeon_mem_init_heap_kms)
708 KMS_INVALID_IOCTL(radeon_irq_emit_kms)
709 KMS_INVALID_IOCTL(radeon_irq_wait_kms)
710 KMS_INVALID_IOCTL(radeon_cp_setparam_kms)
711 KMS_INVALID_IOCTL(radeon_surface_alloc_kms)
712 KMS_INVALID_IOCTL(radeon_surface_free_kms)
713 
714 
715 const struct drm_ioctl_desc radeon_ioctls_kms[] = {
716 	DRM_IOCTL_DEF_DRV(RADEON_CP_INIT, radeon_cp_init_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
717 	DRM_IOCTL_DEF_DRV(RADEON_CP_START, radeon_cp_start_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
718 	DRM_IOCTL_DEF_DRV(RADEON_CP_STOP, radeon_cp_stop_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
719 	DRM_IOCTL_DEF_DRV(RADEON_CP_RESET, radeon_cp_reset_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
720 	DRM_IOCTL_DEF_DRV(RADEON_CP_IDLE, radeon_cp_idle_kms, DRM_AUTH),
721 	DRM_IOCTL_DEF_DRV(RADEON_CP_RESUME, radeon_cp_resume_kms, DRM_AUTH),
722 	DRM_IOCTL_DEF_DRV(RADEON_RESET, radeon_engine_reset_kms, DRM_AUTH),
723 	DRM_IOCTL_DEF_DRV(RADEON_FULLSCREEN, radeon_fullscreen_kms, DRM_AUTH),
724 	DRM_IOCTL_DEF_DRV(RADEON_SWAP, radeon_cp_swap_kms, DRM_AUTH),
725 	DRM_IOCTL_DEF_DRV(RADEON_CLEAR, radeon_cp_clear_kms, DRM_AUTH),
726 	DRM_IOCTL_DEF_DRV(RADEON_VERTEX, radeon_cp_vertex_kms, DRM_AUTH),
727 	DRM_IOCTL_DEF_DRV(RADEON_INDICES, radeon_cp_indices_kms, DRM_AUTH),
728 	DRM_IOCTL_DEF_DRV(RADEON_TEXTURE, radeon_cp_texture_kms, DRM_AUTH),
729 	DRM_IOCTL_DEF_DRV(RADEON_STIPPLE, radeon_cp_stipple_kms, DRM_AUTH),
730 	DRM_IOCTL_DEF_DRV(RADEON_INDIRECT, radeon_cp_indirect_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
731 	DRM_IOCTL_DEF_DRV(RADEON_VERTEX2, radeon_cp_vertex2_kms, DRM_AUTH),
732 	DRM_IOCTL_DEF_DRV(RADEON_CMDBUF, radeon_cp_cmdbuf_kms, DRM_AUTH),
733 	DRM_IOCTL_DEF_DRV(RADEON_GETPARAM, radeon_cp_getparam_kms, DRM_AUTH),
734 	DRM_IOCTL_DEF_DRV(RADEON_FLIP, radeon_cp_flip_kms, DRM_AUTH),
735 	DRM_IOCTL_DEF_DRV(RADEON_ALLOC, radeon_mem_alloc_kms, DRM_AUTH),
736 	DRM_IOCTL_DEF_DRV(RADEON_FREE, radeon_mem_free_kms, DRM_AUTH),
737 	DRM_IOCTL_DEF_DRV(RADEON_INIT_HEAP, radeon_mem_init_heap_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
738 	DRM_IOCTL_DEF_DRV(RADEON_IRQ_EMIT, radeon_irq_emit_kms, DRM_AUTH),
739 	DRM_IOCTL_DEF_DRV(RADEON_IRQ_WAIT, radeon_irq_wait_kms, DRM_AUTH),
740 	DRM_IOCTL_DEF_DRV(RADEON_SETPARAM, radeon_cp_setparam_kms, DRM_AUTH),
741 	DRM_IOCTL_DEF_DRV(RADEON_SURF_ALLOC, radeon_surface_alloc_kms, DRM_AUTH),
742 	DRM_IOCTL_DEF_DRV(RADEON_SURF_FREE, radeon_surface_free_kms, DRM_AUTH),
743 	/* KMS */
744 	DRM_IOCTL_DEF_DRV(RADEON_GEM_INFO, radeon_gem_info_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
745 	DRM_IOCTL_DEF_DRV(RADEON_GEM_CREATE, radeon_gem_create_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
746 	DRM_IOCTL_DEF_DRV(RADEON_GEM_MMAP, radeon_gem_mmap_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
747 	DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_DOMAIN, radeon_gem_set_domain_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
748 	DRM_IOCTL_DEF_DRV(RADEON_GEM_PREAD, radeon_gem_pread_ioctl, DRM_AUTH|DRM_UNLOCKED),
749 	DRM_IOCTL_DEF_DRV(RADEON_GEM_PWRITE, radeon_gem_pwrite_ioctl, DRM_AUTH|DRM_UNLOCKED),
750 	DRM_IOCTL_DEF_DRV(RADEON_GEM_WAIT_IDLE, radeon_gem_wait_idle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
751 	DRM_IOCTL_DEF_DRV(RADEON_CS, radeon_cs_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
752 	DRM_IOCTL_DEF_DRV(RADEON_INFO, radeon_info_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
753 	DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_TILING, radeon_gem_set_tiling_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
754 	DRM_IOCTL_DEF_DRV(RADEON_GEM_GET_TILING, radeon_gem_get_tiling_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
755 	DRM_IOCTL_DEF_DRV(RADEON_GEM_BUSY, radeon_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
756 	DRM_IOCTL_DEF_DRV(RADEON_GEM_VA, radeon_gem_va_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
757 };
758 int radeon_max_kms_ioctl = DRM_ARRAY_SIZE(radeon_ioctls_kms);
759