Skip to content

Commit c3186a4

Browse files
authored
Merge pull request #1053 from tensorflow/sync
Make TPU v4 kernel drivers compatible with linux kernel 6.2.
2 parents 2be44ad + f177d53 commit c3186a4

File tree

6 files changed

+63
-71
lines changed

6 files changed

+63
-71
lines changed

tools/driver/drivers/accel/accel.c

+4
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,11 @@ static struct attribute *accel_dev_attrs[] = {
299299
NULL,
300300
};
301301
ATTRIBUTE_GROUPS(accel_dev);
302+
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0)
302303
static int accel_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
304+
#else
305+
static int accel_dev_uevent(const struct device *dev, struct kobj_uevent_env *env)
306+
#endif
303307
{
304308
struct accel_dev *adev = to_accel_dev(dev);
305309
int retval = 0;

tools/driver/drivers/asic_sw/asic_fw_device_owner_accessor.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
#ifndef _DRIVERS_ASIC_SW_ASIC_FW_DEVICE_OWNER_ACCESSOR_H_
66
#define _DRIVERS_ASIC_SW_ASIC_FW_DEVICE_OWNER_ACCESSOR_H_
77
#include "drivers/gasket/gasket_types.h"
8-
static inline uint64 asic_fw_device_owner_value(const uint64 reg_value)
8+
static inline uint64_t asic_fw_device_owner_value(const uint64_t reg_value)
99
{
10-
return (uint64)((((reg_value >> 0) & 0xffffffffffffffffULL) << 0));
10+
return (uint64_t)((((reg_value >> 0) & 0xffffffffffffffffULL) << 0));
1111
}
12-
static inline int set_asic_fw_device_owner_value(uint64 *reg_value,
13-
uint64 value)
12+
static inline int set_asic_fw_device_owner_value(uint64_t *reg_value,
13+
uint64_t value)
1414
{
15-
if (value & ~(0xffffffffffffffffULL))
15+
if ((uint64_t)value < 0x0ULL || (uint64_t)value > 0xffffffffffffffffULL)
1616
return 1;
1717
(*reg_value) = ((*reg_value) & ~((0xffffffffffffffffULL) << 0)) |
1818
(((value >> 0) & (0xffffffffffffffffULL)) << 0);

tools/driver/drivers/asic_sw/asic_fw_indirect_accessor.h

+42-39
Original file line numberDiff line numberDiff line change
@@ -12,57 +12,59 @@ enum asic_fw_indirect_accessor_status_status_value {
1212
};
1313
typedef enum asic_fw_indirect_accessor_status_status_value
1414
asic_fw_indirect_accessor_status_status_value;
15-
static inline uint64
16-
asic_fw_indirect_accessor_version_version(const uint64 reg_value)
15+
static inline uint64_t
16+
asic_fw_indirect_accessor_version_version(const uint64_t reg_value)
1717
{
18-
return (uint64)((((reg_value >> 0) & 0xffffffffffffffffULL) << 0));
18+
return (uint64_t)((((reg_value >> 0) & 0xffffffffffffffffULL) << 0));
1919
}
2020
static inline int
21-
set_asic_fw_indirect_accessor_version_version(uint64 *reg_value, uint64 value)
21+
set_asic_fw_indirect_accessor_version_version(uint64_t *reg_value,
22+
uint64_t value)
2223
{
23-
if (value & ~(0xffffffffffffffffULL))
24+
if ((uint64_t)value < 0x0ULL || (uint64_t)value > 0xffffffffffffffffULL)
2425
return 1;
2526
(*reg_value) = ((*reg_value) & ~((0xffffffffffffffffULL) << 0)) |
2627
(((value >> 0) & (0xffffffffffffffffULL)) << 0);
2728
return 0;
2829
}
29-
static inline uint64
30-
asic_fw_indirect_accessor_address_address(const uint64 reg_value)
30+
static inline uint64_t
31+
asic_fw_indirect_accessor_address_address(const uint64_t reg_value)
3132
{
32-
return (uint64)((((reg_value >> 0) & 0xffffffffffffffffULL) << 0));
33+
return (uint64_t)((((reg_value >> 0) & 0xffffffffffffffffULL) << 0));
3334
}
3435
static inline int
35-
set_asic_fw_indirect_accessor_address_address(uint64 *reg_value, uint64 value)
36+
set_asic_fw_indirect_accessor_address_address(uint64_t *reg_value,
37+
uint64_t value)
3638
{
37-
if (value & ~(0xffffffffffffffffULL))
39+
if ((uint64_t)value < 0x0ULL || (uint64_t)value > 0xffffffffffffffffULL)
3840
return 1;
3941
(*reg_value) = ((*reg_value) & ~((0xffffffffffffffffULL) << 0)) |
4042
(((value >> 0) & (0xffffffffffffffffULL)) << 0);
4143
return 0;
4244
}
43-
static inline uint8
44-
asic_fw_indirect_accessor_control_write(const uint64 reg_value)
45+
static inline uint8_t
46+
asic_fw_indirect_accessor_control_write(const uint64_t reg_value)
4547
{
46-
return (uint8)((((reg_value >> 0) & 0x1ULL) << 0));
48+
return (uint8_t)((((reg_value >> 0) & 0x1ULL) << 0));
4749
}
48-
static inline int set_asic_fw_indirect_accessor_control_write(uint64 *reg_value,
49-
uint8 value)
50+
static inline int
51+
set_asic_fw_indirect_accessor_control_write(uint64_t *reg_value, uint8_t value)
5052
{
51-
if (value & ~(0x1ULL))
53+
if ((uint64_t)value < 0x0ULL || (uint64_t)value > 0x1ULL)
5254
return 1;
5355
(*reg_value) = ((*reg_value) & ~((0x1ULL) << 0)) |
5456
(((value >> 0) & (0x1ULL)) << 0);
5557
return 0;
5658
}
57-
static inline uint8
58-
asic_fw_indirect_accessor_control_read(const uint64 reg_value)
59+
static inline uint8_t
60+
asic_fw_indirect_accessor_control_read(const uint64_t reg_value)
5961
{
60-
return (uint8)((((reg_value >> 1) & 0x1ULL) << 0));
62+
return (uint8_t)((((reg_value >> 1) & 0x1ULL) << 0));
6163
}
62-
static inline int set_asic_fw_indirect_accessor_control_read(uint64 *reg_value,
63-
uint8 value)
64+
static inline int
65+
set_asic_fw_indirect_accessor_control_read(uint64_t *reg_value, uint8_t value)
6466
{
65-
if (value & ~(0x1ULL))
67+
if ((uint64_t)value < 0x0ULL || (uint64_t)value > 0x1ULL)
6668
return 1;
6769
(*reg_value) = ((*reg_value) & ~((0x1ULL) << 1)) |
6870
(((value >> 0) & (0x1ULL)) << 1);
@@ -97,44 +99,45 @@ static inline const char *asic_fw_indirect_accessor_status_status_value_name(
9799
return "UNKNOWN VALUE";
98100
}
99101
static inline asic_fw_indirect_accessor_status_status_value
100-
asic_fw_indirect_accessor_status_status(const uint64 reg_value)
102+
asic_fw_indirect_accessor_status_status(const uint64_t reg_value)
101103
{
102-
return (asic_fw_indirect_accessor_status_status_value)(
103-
(((reg_value >> 0) & 0xffULL) << 0));
104+
return (asic_fw_indirect_accessor_status_status_value)((
105+
((reg_value >> 0) & 0xffULL) << 0));
104106
}
105107
static inline int set_asic_fw_indirect_accessor_status_status(
106-
uint64 *reg_value, asic_fw_indirect_accessor_status_status_value value)
108+
uint64_t *reg_value,
109+
asic_fw_indirect_accessor_status_status_value value)
107110
{
108-
if (value & ~(0xffULL))
111+
if ((uint64_t)value < 0x0ULL || (uint64_t)value > 0xffULL)
109112
return 1;
110113
(*reg_value) = ((*reg_value) & ~((0xffULL) << 0)) |
111114
(((value >> 0) & (0xffULL)) << 0);
112115
return 0;
113116
}
114-
static inline uint8
115-
asic_fw_indirect_accessor_status_chip_specific_status(const uint64 reg_value)
117+
static inline uint8_t
118+
asic_fw_indirect_accessor_status_chip_specific_status(const uint64_t reg_value)
116119
{
117-
return (uint8)((((reg_value >> 8) & 0xffULL) << 0));
120+
return (uint8_t)((((reg_value >> 8) & 0xffULL) << 0));
118121
}
119122
static inline int
120-
set_asic_fw_indirect_accessor_status_chip_specific_status(uint64 *reg_value,
121-
uint8 value)
123+
set_asic_fw_indirect_accessor_status_chip_specific_status(uint64_t *reg_value,
124+
uint8_t value)
122125
{
123-
if (value & ~(0xffULL))
126+
if ((uint64_t)value < 0x0ULL || (uint64_t)value > 0xffULL)
124127
return 1;
125128
(*reg_value) = ((*reg_value) & ~((0xffULL) << 8)) |
126129
(((value >> 0) & (0xffULL)) << 8);
127130
return 0;
128131
}
129-
static inline uint64
130-
asic_fw_indirect_accessor_value_value(const uint64 reg_value)
132+
static inline uint64_t
133+
asic_fw_indirect_accessor_value_value(const uint64_t reg_value)
131134
{
132-
return (uint64)((((reg_value >> 0) & 0xffffffffffffffffULL) << 0));
135+
return (uint64_t)((((reg_value >> 0) & 0xffffffffffffffffULL) << 0));
133136
}
134-
static inline int set_asic_fw_indirect_accessor_value_value(uint64 *reg_value,
135-
uint64 value)
137+
static inline int set_asic_fw_indirect_accessor_value_value(uint64_t *reg_value,
138+
uint64_t value)
136139
{
137-
if (value & ~(0xffffffffffffffffULL))
140+
if ((uint64_t)value < 0x0ULL || (uint64_t)value > 0xffffffffffffffffULL)
138141
return 1;
139142
(*reg_value) = ((*reg_value) & ~((0xffffffffffffffffULL) << 0)) |
140143
(((value >> 0) & (0xffffffffffffffffULL)) << 0);

tools/driver/drivers/gasket/gasket_core.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,8 @@ static void gasket_setup_pci_iommu(struct gasket_dev *gasket_dev)
552552
} else if (driver_desc->iommu_mappings == GASKET_IOMMU_PREFER) {
553553
#if 0
554554
#else
555-
gasket_log_warn(gasket_dev,
556-
"IOMMU Mappings: Cannot enable");
555+
gasket_log_warn(gasket_dev,
556+
"IOMMU Mappings: Cannot enable");
557557
#endif
558558
}
559559
}
@@ -1255,6 +1255,8 @@ int gasket_mm_unmap_region(
12551255
int bar_index;
12561256
ulong bar_offset;
12571257
ulong virt_offset;
1258+
ulong address;
1259+
ulong size;
12581260
struct gasket_mappable_region mappable_region;
12591261
if (vma->vm_private_data != gasket_dev)
12601262
return -EINVAL;
@@ -1279,9 +1281,14 @@ int gasket_mm_unmap_region(
12791281
if (!gasket_mm_get_mapping_addrs(map_region, bar_offset,
12801282
vma->vm_end - vma->vm_start, &mappable_region, &virt_offset))
12811283
return 1;
1282-
zap_vma_ptes(vma, vma->vm_start + virt_offset,
1283-
DIV_ROUND_UP(mappable_region.length_bytes, PAGE_SIZE) *
1284-
PAGE_SIZE); return 0;
1284+
address = vma->vm_start + virt_offset;
1285+
size = DIV_ROUND_UP(mappable_region.length_bytes, PAGE_SIZE) *
1286+
PAGE_SIZE;
1287+
if (address < vma->vm_start || address + size > vma->vm_end ||
1288+
!(vma->vm_flags & VM_PFNMAP))
1289+
return -1;
1290+
zap_vma_ptes(vma, address, size);
1291+
return 0;
12851292
}
12861293
EXPORT_SYMBOL(gasket_mm_unmap_region);
12871294
static enum do_map_region_status do_map_region(

tools/driver/drivers/gasket/gasket_dmabuf.c

-6
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ static struct sg_table *gasket_dma_buf_ops_map(
133133
phys_addr = gasket_dbuf->mmap_offset -
134134
gasket_dev->driver_desc->bar_descriptions[bar_index].base +
135135
gasket_dev->bar_data[bar_index].phys_base;
136-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
137136
addr = dma_map_resource(attachment->dev, phys_addr, dbuf->size,
138137
direction, DMA_ATTR_SKIP_CPU_SYNC);
139138
ret = dma_mapping_error(attachment->dev, addr);
@@ -143,9 +142,6 @@ static struct sg_table *gasket_dma_buf_ops_map(
143142
sg_free_table(sgt);
144143
goto error_alloc_table;
145144
}
146-
#else
147-
addr = phys_addr;
148-
#endif
149145
sg_set_page(sgt->sgl, NULL, dbuf->size, 0);
150146
sg_dma_address(sgt->sgl) = addr;
151147
sg_dma_len(sgt->sgl) = dbuf->size;
@@ -157,15 +153,13 @@ static struct sg_table *gasket_dma_buf_ops_map(
157153
static void gasket_dma_buf_ops_unmap(struct dma_buf_attachment *attachment,
158154
struct sg_table *sgt, enum dma_data_direction direction)
159155
{
160-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
161156
struct scatterlist *sg;
162157
int i;
163158
for_each_sg((sgt)->sgl, sg, (sgt)->orig_nents, i)
164159
{
165160
dma_unmap_resource(attachment->dev, sg_dma_address(sg),
166161
sg_dma_len(sg), direction, DMA_ATTR_SKIP_CPU_SYNC);
167162
}
168-
#endif
169163
sg_free_table(sgt);
170164
kfree(sgt);
171165
}

tools/driver/drivers/gasket/gasket_page_table.c

-16
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
#include <linux/pagemap.h>
1313
#include <linux/version.h>
1414
#include <linux/vmalloc.h>
15-
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 7, 19)
1615
#include <linux/dma-resv.h>
17-
#endif
1816
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 16, 0)
1917
MODULE_IMPORT_NS(DMA_BUF);
2018
#endif
@@ -915,7 +913,6 @@ static size_t gasket_sgt_get_contiguous_size(
915913
}
916914
return sz;
917915
}
918-
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 7, 19)
919916
static void gasket_page_table_dma_buf_move_notify(
920917
struct dma_buf_attachment *attachment)
921918
{
@@ -941,7 +938,6 @@ static const struct dma_buf_attach_ops gasket_dma_buf_attach_ops = {
941938
.allow_peer2peer = true,
942939
.move_notify = gasket_page_table_dma_buf_move_notify,
943940
};
944-
#endif
945941
static struct gasket_sgt_mapping *gasket_page_table_import_dma_buf(
946942
struct gasket_page_table *pg_tbl, int dma_buf_fd)
947943
{
@@ -971,27 +967,19 @@ static struct gasket_sgt_mapping *gasket_page_table_import_dma_buf(
971967
ret = -ENOMEM;
972968
goto failed_mapping_alloc;
973969
}
974-
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 7, 19)
975970
mapping->dbuf_attach = dma_buf_dynamic_attach(dbuf,
976971
&gasket_dev->pci_dev->dev, &gasket_dma_buf_attach_ops, pg_tbl);
977-
#else
978-
mapping->dbuf_attach = dma_buf_attach(dbuf, &gasket_dev->pci_dev->dev);
979-
#endif
980972
if (IS_ERR(mapping->dbuf_attach)) {
981973
ret = PTR_ERR(mapping->dbuf_attach);
982974
gasket_log_error(
983975
gasket_dev, "failed to attach to the dma_buf: %d", ret);
984976
goto failed_attach;
985977
}
986978
mapping->size = dbuf->size;
987-
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 7, 19)
988979
dma_resv_lock(dbuf->resv, NULL);
989-
#endif
990980
mapping->sgt =
991981
dma_buf_map_attachment(mapping->dbuf_attach, DMA_BIDIRECTIONAL);
992-
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 7, 19)
993982
dma_resv_unlock(dbuf->resv);
994-
#endif
995983
if (IS_ERR(mapping->sgt)) {
996984
ret = PTR_ERR(mapping->sgt);
997985
gasket_log_error(gasket_dev,
@@ -1016,14 +1004,10 @@ static void gasket_page_table_detach_sgt_mapping(
10161004
list_del_init(&mapping->entry);
10171005
if (mapping->dbuf_attach) {
10181006
dbuf = mapping->dbuf_attach->dmabuf;
1019-
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 7, 19)
10201007
dma_resv_lock(dbuf->resv, NULL);
1021-
#endif
10221008
dma_buf_unmap_attachment(
10231009
mapping->dbuf_attach, mapping->sgt, DMA_BIDIRECTIONAL);
1024-
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 7, 19)
10251010
dma_resv_unlock(dbuf->resv);
1026-
#endif
10271011
dma_buf_detach(dbuf, mapping->dbuf_attach);
10281012
dma_buf_put(dbuf);
10291013
}

0 commit comments

Comments
 (0)