1/*
2 * Copyright 2012 Tilera Corporation. All Rights Reserved.
3 *
4 *   This program is free software; you can redistribute it and/or
5 *   modify it under the terms of the GNU General Public License
6 *   as published by the Free Software Foundation, version 2.
7 *
8 *   This program is distributed in the hope that it will be useful, but
9 *   WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 *   NON INFRINGEMENT.  See the GNU General Public License for
12 *   more details.
13 *
14 * TILE-Gx IORPC support for kernel I/O drivers.
15 */
16
17#include <linux/mmzone.h>
18#include <linux/module.h>
19#include <linux/io.h>
20#include <gxio/iorpc_globals.h>
21#include <gxio/kiorpc.h>
22
23#ifdef DEBUG_IORPC
24#define TRACE(FMT, ...) pr_info(SIMPLE_MSG_LINE FMT, ## __VA_ARGS__)
25#else
26#define TRACE(...)
27#endif
28
29/* Create kernel-VA-space MMIO mapping for an on-chip IO device. */
30void __iomem *iorpc_ioremap(int hv_fd, resource_size_t offset,
31			    unsigned long size)
32{
33	pgprot_t mmio_base, prot = { 0 };
34	unsigned long pfn;
35	int err;
36
37	/* Look up the shim's lotar and base PA. */
38	err = __iorpc_get_mmio_base(hv_fd, &mmio_base);
39	if (err) {
40		TRACE("get_mmio_base() failure: %d\n", err);
41		return NULL;
42	}
43
44	/* Make sure the HV driver approves of our offset and size. */
45	err = __iorpc_check_mmio_offset(hv_fd, offset, size);
46	if (err) {
47		TRACE("check_mmio_offset() failure: %d\n", err);
48		return NULL;
49	}
50
51	/*
52	 * mmio_base contains a base pfn and homing coordinates.  Turn
53	 * it into an MMIO pgprot and offset pfn.
54	 */
55	prot = hv_pte_set_lotar(prot, hv_pte_get_lotar(mmio_base));
56	pfn = pte_pfn(mmio_base) + PFN_DOWN(offset);
57
58	return ioremap_prot(PFN_PHYS(pfn), size, prot);
59}
60
61EXPORT_SYMBOL(iorpc_ioremap);
62