1226048Sobrien// SPDX-License-Identifier: GPL-2.0-or-later
268349Sobrien/*
3267843Sdelphij * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
468349Sobrien */
568349Sobrien#include <linux/kernel.h>
668349Sobrien#include <linux/export.h>
768349Sobrien#include <linux/init.h>
868349Sobrien#include <linux/acpi.h>
9186690Sobrien#include <linux/types.h>
10186690Sobrien#include <linux/pci.h>
11186690Sobrien#include <linux/vgaarb.h>
12186690Sobrien#include <asm/cacheflush.h>
13186690Sobrien#include <asm/loongson.h>
14186690Sobrien
15186690Sobrien#define PCI_DEVICE_ID_LOONGSON_HOST     0x7a00
16186690Sobrien#define PCI_DEVICE_ID_LOONGSON_DC1      0x7a06
17186690Sobrien#define PCI_DEVICE_ID_LOONGSON_DC2      0x7a36
18186690Sobrien
19267843Sdelphijint raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
20192348Sdelphij						int reg, int len, u32 *val)
21267843Sdelphij{
22192348Sdelphij	struct pci_bus *bus_tmp = pci_find_bus(domain, bus);
2368349Sobrien
2468349Sobrien	if (bus_tmp)
25186690Sobrien		return bus_tmp->ops->read(bus_tmp, devfn, reg, len, val);
26186690Sobrien	return -EINVAL;
27186690Sobrien}
28186690Sobrien
29186690Sobrienint raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
30186690Sobrien						int reg, int len, u32 val)
31186690Sobrien{
32186690Sobrien	struct pci_bus *bus_tmp = pci_find_bus(domain, bus);
33186690Sobrien
34186690Sobrien	if (bus_tmp)
35186690Sobrien		return bus_tmp->ops->write(bus_tmp, devfn, reg, len, val);
3668349Sobrien	return -EINVAL;
3768349Sobrien}
3868349Sobrien
39phys_addr_t mcfg_addr_init(int node)
40{
41	return (((u64)node << 44) | MCFG_EXT_PCICFG_BASE);
42}
43
44static int __init pcibios_init(void)
45{
46	unsigned int lsize;
47
48	/*
49	 * Set PCI cacheline size to that of the last level in the
50	 * cache hierarchy.
51	 */
52	lsize = cpu_last_level_cache_line_size();
53
54	BUG_ON(!lsize);
55
56	pci_dfl_cache_line_size = lsize >> 2;
57
58	pr_debug("PCI: pci_cache_line_size set to %d bytes\n", lsize);
59
60	return 0;
61}
62
63subsys_initcall(pcibios_init);
64
65int pcibios_device_add(struct pci_dev *dev)
66{
67	int id;
68	struct irq_domain *dom;
69
70	id = pci_domain_nr(dev->bus);
71	dom = irq_find_matching_fwnode(get_pch_msi_handle(id), DOMAIN_BUS_PCI_MSI);
72	dev_set_msi_domain(&dev->dev, dom);
73
74	return 0;
75}
76
77int pcibios_alloc_irq(struct pci_dev *dev)
78{
79	if (acpi_disabled)
80		return 0;
81	if (pci_dev_msi_enabled(dev))
82		return 0;
83	return acpi_pci_irq_enable(dev);
84}
85
86static void pci_fixup_vgadev(struct pci_dev *pdev)
87{
88	struct pci_dev *devp = NULL;
89
90	while ((devp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, devp))) {
91		if (devp->vendor != PCI_VENDOR_ID_LOONGSON) {
92			vga_set_default_device(devp);
93			dev_info(&pdev->dev,
94				"Overriding boot device as %X:%X\n",
95				devp->vendor, devp->device);
96		}
97	}
98}
99DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON, PCI_DEVICE_ID_LOONGSON_DC1, pci_fixup_vgadev);
100DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LOONGSON, PCI_DEVICE_ID_LOONGSON_DC2, pci_fixup_vgadev);
101