1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright (C) 2013-2017 Oracle Corporation
4  * This file is based on ast_ttm.c
5  * Copyright 2012 Red Hat Inc.
6  * Authors: Dave Airlie <airlied@redhat.com>
7  *          Michael Thayer <michael.thayer@oracle.com>
8  */
9 #include <linux/pci.h>
10 #include <drm/drm_file.h>
11 #include "vbox_drv.h"
12 
13 int vbox_mm_init(struct vbox_private *vbox)
14 {
15 	struct drm_vram_mm *vmm;
16 	int ret;
17 	struct drm_device *dev = &vbox->ddev;
18 
19 	vmm = drm_vram_helper_alloc_mm(dev, pci_resource_start(dev->pdev, 0),
20 				       vbox->available_vram_size);
21 	if (IS_ERR(vmm)) {
22 		ret = PTR_ERR(vmm);
23 		DRM_ERROR("Error initializing VRAM MM; %d\n", ret);
24 		return ret;
25 	}
26 
27 #ifdef DRM_MTRR_WC
28 	vbox->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
29 				     pci_resource_len(dev->pdev, 0),
30 				     DRM_MTRR_WC);
31 #else
32 	vbox->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
33 					 pci_resource_len(dev->pdev, 0));
34 #endif
35 	return 0;
36 }
37 
38 void vbox_mm_fini(struct vbox_private *vbox)
39 {
40 #ifdef DRM_MTRR_WC
41 	drm_mtrr_del(vbox->fb_mtrr,
42 		     pci_resource_start(vbox->ddev.pdev, 0),
43 		     pci_resource_len(vbox->ddev.pdev, 0), DRM_MTRR_WC);
44 #else
45 	arch_phys_wc_del(vbox->fb_mtrr);
46 #endif
47 	drm_vram_helper_release_mm(&vbox->ddev);
48 }
49