xref: /openbmc/linux/drivers/gpu/drm/ast/ast_mm.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
148fde424SThomas Zimmermann /*
248fde424SThomas Zimmermann  * Copyright 2012 Red Hat Inc.
348fde424SThomas Zimmermann  *
448fde424SThomas Zimmermann  * Permission is hereby granted, free of charge, to any person obtaining a
548fde424SThomas Zimmermann  * copy of this software and associated documentation files (the
648fde424SThomas Zimmermann  * "Software"), to deal in the Software without restriction, including
748fde424SThomas Zimmermann  * without limitation the rights to use, copy, modify, merge, publish,
848fde424SThomas Zimmermann  * distribute, sub license, and/or sell copies of the Software, and to
948fde424SThomas Zimmermann  * permit persons to whom the Software is furnished to do so, subject to
1048fde424SThomas Zimmermann  * the following conditions:
1148fde424SThomas Zimmermann  *
1248fde424SThomas Zimmermann  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1348fde424SThomas Zimmermann  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1448fde424SThomas Zimmermann  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
1548fde424SThomas Zimmermann  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
1648fde424SThomas Zimmermann  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1748fde424SThomas Zimmermann  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
1848fde424SThomas Zimmermann  * USE OR OTHER DEALINGS IN THE SOFTWARE.
1948fde424SThomas Zimmermann  *
2048fde424SThomas Zimmermann  * The above copyright notice and this permission notice (including the
2148fde424SThomas Zimmermann  * next paragraph) shall be included in all copies or substantial portions
2248fde424SThomas Zimmermann  * of the Software.
2348fde424SThomas Zimmermann  *
2448fde424SThomas Zimmermann  */
2548fde424SThomas Zimmermann /*
2648fde424SThomas Zimmermann  * Authors: Dave Airlie <airlied@redhat.com>
2748fde424SThomas Zimmermann  */
2848fde424SThomas Zimmermann 
2948fde424SThomas Zimmermann #include <linux/pci.h>
3048fde424SThomas Zimmermann 
3103ba7e00SThomas Zimmermann #include <drm/drm_managed.h>
3203ba7e00SThomas Zimmermann #include <drm/drm_print.h>
3348fde424SThomas Zimmermann 
3448fde424SThomas Zimmermann #include "ast_drv.h"
3548fde424SThomas Zimmermann 
ast_get_vram_size(struct ast_device * ast)36*37b42cf9SThomas Zimmermann static u32 ast_get_vram_size(struct ast_device *ast)
370149e780SThomas Zimmermann {
380149e780SThomas Zimmermann 	u8 jreg;
390149e780SThomas Zimmermann 	u32 vram_size;
400149e780SThomas Zimmermann 
410149e780SThomas Zimmermann 	vram_size = AST_VIDMEM_DEFAULT_SIZE;
420149e780SThomas Zimmermann 	jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
430149e780SThomas Zimmermann 	switch (jreg & 3) {
440149e780SThomas Zimmermann 	case 0:
450149e780SThomas Zimmermann 		vram_size = AST_VIDMEM_SIZE_8M;
460149e780SThomas Zimmermann 		break;
470149e780SThomas Zimmermann 	case 1:
480149e780SThomas Zimmermann 		vram_size = AST_VIDMEM_SIZE_16M;
490149e780SThomas Zimmermann 		break;
500149e780SThomas Zimmermann 	case 2:
510149e780SThomas Zimmermann 		vram_size = AST_VIDMEM_SIZE_32M;
520149e780SThomas Zimmermann 		break;
530149e780SThomas Zimmermann 	case 3:
540149e780SThomas Zimmermann 		vram_size = AST_VIDMEM_SIZE_64M;
550149e780SThomas Zimmermann 		break;
560149e780SThomas Zimmermann 	}
570149e780SThomas Zimmermann 
580149e780SThomas Zimmermann 	jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xff);
590149e780SThomas Zimmermann 	switch (jreg & 0x03) {
600149e780SThomas Zimmermann 	case 1:
610149e780SThomas Zimmermann 		vram_size -= 0x100000;
620149e780SThomas Zimmermann 		break;
630149e780SThomas Zimmermann 	case 2:
640149e780SThomas Zimmermann 		vram_size -= 0x200000;
650149e780SThomas Zimmermann 		break;
660149e780SThomas Zimmermann 	case 3:
670149e780SThomas Zimmermann 		vram_size -= 0x400000;
680149e780SThomas Zimmermann 		break;
690149e780SThomas Zimmermann 	}
700149e780SThomas Zimmermann 
710149e780SThomas Zimmermann 	return vram_size;
720149e780SThomas Zimmermann }
730149e780SThomas Zimmermann 
ast_mm_init(struct ast_device * ast)74*37b42cf9SThomas Zimmermann int ast_mm_init(struct ast_device *ast)
7548fde424SThomas Zimmermann {
76e0f5a738SThomas Zimmermann 	struct drm_device *dev = &ast->base;
7746fb883cSThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(dev->dev);
7823b405bfSThomas Zimmermann 	resource_size_t base, size;
790149e780SThomas Zimmermann 	u32 vram_size;
8048fde424SThomas Zimmermann 
8123b405bfSThomas Zimmermann 	base = pci_resource_start(pdev, 0);
8223b405bfSThomas Zimmermann 	size = pci_resource_len(pdev, 0);
8323b405bfSThomas Zimmermann 
8423b405bfSThomas Zimmermann 	/* Don't fail on errors, but performance might be reduced. */
8523b405bfSThomas Zimmermann 	devm_arch_io_reserve_memtype_wc(dev->dev, base, size);
8623b405bfSThomas Zimmermann 	devm_arch_phys_wc_add(dev->dev, base, size);
8723b405bfSThomas Zimmermann 
880149e780SThomas Zimmermann 	vram_size = ast_get_vram_size(ast);
890149e780SThomas Zimmermann 
90f2fa5a99SThomas Zimmermann 	ast->vram = devm_ioremap_wc(dev->dev, base, vram_size);
91f2fa5a99SThomas Zimmermann 	if (!ast->vram)
92f2fa5a99SThomas Zimmermann 		return -ENOMEM;
93f2fa5a99SThomas Zimmermann 
94f2fa5a99SThomas Zimmermann 	ast->vram_base = base;
95f2fa5a99SThomas Zimmermann 	ast->vram_size = vram_size;
96f2fa5a99SThomas Zimmermann 	ast->vram_fb_available = vram_size;
9748fde424SThomas Zimmermann 
9823b405bfSThomas Zimmermann 	return 0;
9948fde424SThomas Zimmermann }
100