i915_gem.c 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_gem_clflush.h"
  32. #include "i915_vgpu.h"
  33. #include "i915_trace.h"
  34. #include "intel_drv.h"
  35. #include "intel_frontbuffer.h"
  36. #include "intel_mocs.h"
  37. #include <linux/dma-fence-array.h>
  38. #include <linux/kthread.h>
  39. #include <linux/reservation.h>
  40. #include <linux/shmem_fs.h>
  41. #include <linux/slab.h>
  42. #include <linux/stop_machine.h>
  43. #include <linux/swap.h>
  44. #include <linux/pci.h>
  45. #include <linux/dma-buf.h>
  46. static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
  47. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  48. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  49. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  50. {
  51. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  52. return false;
  53. if (!i915_gem_object_is_coherent(obj))
  54. return true;
  55. return obj->pin_display;
  56. }
  57. static int
  58. insert_mappable_node(struct i915_ggtt *ggtt,
  59. struct drm_mm_node *node, u32 size)
  60. {
  61. memset(node, 0, sizeof(*node));
  62. return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
  63. size, 0, I915_COLOR_UNEVICTABLE,
  64. 0, ggtt->mappable_end,
  65. DRM_MM_INSERT_LOW);
  66. }
  67. static void
  68. remove_mappable_node(struct drm_mm_node *node)
  69. {
  70. drm_mm_remove_node(node);
  71. }
  72. /* some bookkeeping */
  73. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  74. u64 size)
  75. {
  76. spin_lock(&dev_priv->mm.object_stat_lock);
  77. dev_priv->mm.object_count++;
  78. dev_priv->mm.object_memory += size;
  79. spin_unlock(&dev_priv->mm.object_stat_lock);
  80. }
  81. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  82. u64 size)
  83. {
  84. spin_lock(&dev_priv->mm.object_stat_lock);
  85. dev_priv->mm.object_count--;
  86. dev_priv->mm.object_memory -= size;
  87. spin_unlock(&dev_priv->mm.object_stat_lock);
  88. }
  89. static int
  90. i915_gem_wait_for_error(struct i915_gpu_error *error)
  91. {
  92. int ret;
  93. might_sleep();
  94. /*
  95. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  96. * userspace. If it takes that long something really bad is going on and
  97. * we should simply try to bail out and fail as gracefully as possible.
  98. */
  99. ret = wait_event_interruptible_timeout(error->reset_queue,
  100. !i915_reset_backoff(error),
  101. I915_RESET_TIMEOUT);
  102. if (ret == 0) {
  103. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  104. return -EIO;
  105. } else if (ret < 0) {
  106. return ret;
  107. } else {
  108. return 0;
  109. }
  110. }
  111. int i915_mutex_lock_interruptible(struct drm_device *dev)
  112. {
  113. struct drm_i915_private *dev_priv = to_i915(dev);
  114. int ret;
  115. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  116. if (ret)
  117. return ret;
  118. ret = mutex_lock_interruptible(&dev->struct_mutex);
  119. if (ret)
  120. return ret;
  121. return 0;
  122. }
  123. int
  124. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  125. struct drm_file *file)
  126. {
  127. struct drm_i915_private *dev_priv = to_i915(dev);
  128. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  129. struct drm_i915_gem_get_aperture *args = data;
  130. struct i915_vma *vma;
  131. size_t pinned;
  132. pinned = 0;
  133. mutex_lock(&dev->struct_mutex);
  134. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  135. if (i915_vma_is_pinned(vma))
  136. pinned += vma->node.size;
  137. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  138. if (i915_vma_is_pinned(vma))
  139. pinned += vma->node.size;
  140. mutex_unlock(&dev->struct_mutex);
  141. args->aper_size = ggtt->base.total;
  142. args->aper_available_size = args->aper_size - pinned;
  143. return 0;
  144. }
  145. static struct sg_table *
  146. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  147. {
  148. struct address_space *mapping = obj->base.filp->f_mapping;
  149. drm_dma_handle_t *phys;
  150. struct sg_table *st;
  151. struct scatterlist *sg;
  152. char *vaddr;
  153. int i;
  154. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  155. return ERR_PTR(-EINVAL);
  156. /* Always aligning to the object size, allows a single allocation
  157. * to handle all possible callers, and given typical object sizes,
  158. * the alignment of the buddy allocation will naturally match.
  159. */
  160. phys = drm_pci_alloc(obj->base.dev,
  161. obj->base.size,
  162. roundup_pow_of_two(obj->base.size));
  163. if (!phys)
  164. return ERR_PTR(-ENOMEM);
  165. vaddr = phys->vaddr;
  166. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  167. struct page *page;
  168. char *src;
  169. page = shmem_read_mapping_page(mapping, i);
  170. if (IS_ERR(page)) {
  171. st = ERR_CAST(page);
  172. goto err_phys;
  173. }
  174. src = kmap_atomic(page);
  175. memcpy(vaddr, src, PAGE_SIZE);
  176. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  177. kunmap_atomic(src);
  178. put_page(page);
  179. vaddr += PAGE_SIZE;
  180. }
  181. i915_gem_chipset_flush(to_i915(obj->base.dev));
  182. st = kmalloc(sizeof(*st), GFP_KERNEL);
  183. if (!st) {
  184. st = ERR_PTR(-ENOMEM);
  185. goto err_phys;
  186. }
  187. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  188. kfree(st);
  189. st = ERR_PTR(-ENOMEM);
  190. goto err_phys;
  191. }
  192. sg = st->sgl;
  193. sg->offset = 0;
  194. sg->length = obj->base.size;
  195. sg_dma_address(sg) = phys->busaddr;
  196. sg_dma_len(sg) = obj->base.size;
  197. obj->phys_handle = phys;
  198. return st;
  199. err_phys:
  200. drm_pci_free(obj->base.dev, phys);
  201. return st;
  202. }
  203. static void
  204. __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
  205. struct sg_table *pages,
  206. bool needs_clflush)
  207. {
  208. GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
  209. if (obj->mm.madv == I915_MADV_DONTNEED)
  210. obj->mm.dirty = false;
  211. if (needs_clflush &&
  212. (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
  213. !i915_gem_object_is_coherent(obj))
  214. drm_clflush_sg(pages);
  215. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  216. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  217. }
  218. static void
  219. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
  220. struct sg_table *pages)
  221. {
  222. __i915_gem_object_release_shmem(obj, pages, false);
  223. if (obj->mm.dirty) {
  224. struct address_space *mapping = obj->base.filp->f_mapping;
  225. char *vaddr = obj->phys_handle->vaddr;
  226. int i;
  227. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  228. struct page *page;
  229. char *dst;
  230. page = shmem_read_mapping_page(mapping, i);
  231. if (IS_ERR(page))
  232. continue;
  233. dst = kmap_atomic(page);
  234. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  235. memcpy(dst, vaddr, PAGE_SIZE);
  236. kunmap_atomic(dst);
  237. set_page_dirty(page);
  238. if (obj->mm.madv == I915_MADV_WILLNEED)
  239. mark_page_accessed(page);
  240. put_page(page);
  241. vaddr += PAGE_SIZE;
  242. }
  243. obj->mm.dirty = false;
  244. }
  245. sg_free_table(pages);
  246. kfree(pages);
  247. drm_pci_free(obj->base.dev, obj->phys_handle);
  248. }
  249. static void
  250. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  251. {
  252. i915_gem_object_unpin_pages(obj);
  253. }
  254. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  255. .get_pages = i915_gem_object_get_pages_phys,
  256. .put_pages = i915_gem_object_put_pages_phys,
  257. .release = i915_gem_object_release_phys,
  258. };
  259. static const struct drm_i915_gem_object_ops i915_gem_object_ops;
  260. int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  261. {
  262. struct i915_vma *vma;
  263. LIST_HEAD(still_in_list);
  264. int ret;
  265. lockdep_assert_held(&obj->base.dev->struct_mutex);
  266. /* Closed vma are removed from the obj->vma_list - but they may
  267. * still have an active binding on the object. To remove those we
  268. * must wait for all rendering to complete to the object (as unbinding
  269. * must anyway), and retire the requests.
  270. */
  271. ret = i915_gem_object_wait(obj,
  272. I915_WAIT_INTERRUPTIBLE |
  273. I915_WAIT_LOCKED |
  274. I915_WAIT_ALL,
  275. MAX_SCHEDULE_TIMEOUT,
  276. NULL);
  277. if (ret)
  278. return ret;
  279. i915_gem_retire_requests(to_i915(obj->base.dev));
  280. while ((vma = list_first_entry_or_null(&obj->vma_list,
  281. struct i915_vma,
  282. obj_link))) {
  283. list_move_tail(&vma->obj_link, &still_in_list);
  284. ret = i915_vma_unbind(vma);
  285. if (ret)
  286. break;
  287. }
  288. list_splice(&still_in_list, &obj->vma_list);
  289. return ret;
  290. }
  291. static long
  292. i915_gem_object_wait_fence(struct dma_fence *fence,
  293. unsigned int flags,
  294. long timeout,
  295. struct intel_rps_client *rps)
  296. {
  297. struct drm_i915_gem_request *rq;
  298. BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
  299. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  300. return timeout;
  301. if (!dma_fence_is_i915(fence))
  302. return dma_fence_wait_timeout(fence,
  303. flags & I915_WAIT_INTERRUPTIBLE,
  304. timeout);
  305. rq = to_request(fence);
  306. if (i915_gem_request_completed(rq))
  307. goto out;
  308. /* This client is about to stall waiting for the GPU. In many cases
  309. * this is undesirable and limits the throughput of the system, as
  310. * many clients cannot continue processing user input/output whilst
  311. * blocked. RPS autotuning may take tens of milliseconds to respond
  312. * to the GPU load and thus incurs additional latency for the client.
  313. * We can circumvent that by promoting the GPU frequency to maximum
  314. * before we wait. This makes the GPU throttle up much more quickly
  315. * (good for benchmarks and user experience, e.g. window animations),
  316. * but at a cost of spending more power processing the workload
  317. * (bad for battery). Not all clients even want their results
  318. * immediately and for them we should just let the GPU select its own
  319. * frequency to maximise efficiency. To prevent a single client from
  320. * forcing the clocks too high for the whole system, we only allow
  321. * each client to waitboost once in a busy period.
  322. */
  323. if (rps) {
  324. if (INTEL_GEN(rq->i915) >= 6)
  325. gen6_rps_boost(rq->i915, rps, rq->emitted_jiffies);
  326. else
  327. rps = NULL;
  328. }
  329. timeout = i915_wait_request(rq, flags, timeout);
  330. out:
  331. if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
  332. i915_gem_request_retire_upto(rq);
  333. if (rps && i915_gem_request_global_seqno(rq) == intel_engine_last_submit(rq->engine)) {
  334. /* The GPU is now idle and this client has stalled.
  335. * Since no other client has submitted a request in the
  336. * meantime, assume that this client is the only one
  337. * supplying work to the GPU but is unable to keep that
  338. * work supplied because it is waiting. Since the GPU is
  339. * then never kept fully busy, RPS autoclocking will
  340. * keep the clocks relatively low, causing further delays.
  341. * Compensate by giving the synchronous client credit for
  342. * a waitboost next time.
  343. */
  344. spin_lock(&rq->i915->rps.client_lock);
  345. list_del_init(&rps->link);
  346. spin_unlock(&rq->i915->rps.client_lock);
  347. }
  348. return timeout;
  349. }
  350. static long
  351. i915_gem_object_wait_reservation(struct reservation_object *resv,
  352. unsigned int flags,
  353. long timeout,
  354. struct intel_rps_client *rps)
  355. {
  356. unsigned int seq = __read_seqcount_begin(&resv->seq);
  357. struct dma_fence *excl;
  358. bool prune_fences = false;
  359. if (flags & I915_WAIT_ALL) {
  360. struct dma_fence **shared;
  361. unsigned int count, i;
  362. int ret;
  363. ret = reservation_object_get_fences_rcu(resv,
  364. &excl, &count, &shared);
  365. if (ret)
  366. return ret;
  367. for (i = 0; i < count; i++) {
  368. timeout = i915_gem_object_wait_fence(shared[i],
  369. flags, timeout,
  370. rps);
  371. if (timeout < 0)
  372. break;
  373. dma_fence_put(shared[i]);
  374. }
  375. for (; i < count; i++)
  376. dma_fence_put(shared[i]);
  377. kfree(shared);
  378. prune_fences = count && timeout >= 0;
  379. } else {
  380. excl = reservation_object_get_excl_rcu(resv);
  381. }
  382. if (excl && timeout >= 0) {
  383. timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps);
  384. prune_fences = timeout >= 0;
  385. }
  386. dma_fence_put(excl);
  387. /* Oportunistically prune the fences iff we know they have *all* been
  388. * signaled and that the reservation object has not been changed (i.e.
  389. * no new fences have been added).
  390. */
  391. if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
  392. if (reservation_object_trylock(resv)) {
  393. if (!__read_seqcount_retry(&resv->seq, seq))
  394. reservation_object_add_excl_fence(resv, NULL);
  395. reservation_object_unlock(resv);
  396. }
  397. }
  398. return timeout;
  399. }
  400. static void __fence_set_priority(struct dma_fence *fence, int prio)
  401. {
  402. struct drm_i915_gem_request *rq;
  403. struct intel_engine_cs *engine;
  404. if (!dma_fence_is_i915(fence))
  405. return;
  406. rq = to_request(fence);
  407. engine = rq->engine;
  408. if (!engine->schedule)
  409. return;
  410. engine->schedule(rq, prio);
  411. }
  412. static void fence_set_priority(struct dma_fence *fence, int prio)
  413. {
  414. /* Recurse once into a fence-array */
  415. if (dma_fence_is_array(fence)) {
  416. struct dma_fence_array *array = to_dma_fence_array(fence);
  417. int i;
  418. for (i = 0; i < array->num_fences; i++)
  419. __fence_set_priority(array->fences[i], prio);
  420. } else {
  421. __fence_set_priority(fence, prio);
  422. }
  423. }
  424. int
  425. i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
  426. unsigned int flags,
  427. int prio)
  428. {
  429. struct dma_fence *excl;
  430. if (flags & I915_WAIT_ALL) {
  431. struct dma_fence **shared;
  432. unsigned int count, i;
  433. int ret;
  434. ret = reservation_object_get_fences_rcu(obj->resv,
  435. &excl, &count, &shared);
  436. if (ret)
  437. return ret;
  438. for (i = 0; i < count; i++) {
  439. fence_set_priority(shared[i], prio);
  440. dma_fence_put(shared[i]);
  441. }
  442. kfree(shared);
  443. } else {
  444. excl = reservation_object_get_excl_rcu(obj->resv);
  445. }
  446. if (excl) {
  447. fence_set_priority(excl, prio);
  448. dma_fence_put(excl);
  449. }
  450. return 0;
  451. }
  452. /**
  453. * Waits for rendering to the object to be completed
  454. * @obj: i915 gem object
  455. * @flags: how to wait (under a lock, for all rendering or just for writes etc)
  456. * @timeout: how long to wait
  457. * @rps: client (user process) to charge for any waitboosting
  458. */
  459. int
  460. i915_gem_object_wait(struct drm_i915_gem_object *obj,
  461. unsigned int flags,
  462. long timeout,
  463. struct intel_rps_client *rps)
  464. {
  465. might_sleep();
  466. #if IS_ENABLED(CONFIG_LOCKDEP)
  467. GEM_BUG_ON(debug_locks &&
  468. !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
  469. !!(flags & I915_WAIT_LOCKED));
  470. #endif
  471. GEM_BUG_ON(timeout < 0);
  472. timeout = i915_gem_object_wait_reservation(obj->resv,
  473. flags, timeout,
  474. rps);
  475. return timeout < 0 ? timeout : 0;
  476. }
  477. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  478. {
  479. struct drm_i915_file_private *fpriv = file->driver_priv;
  480. return &fpriv->rps;
  481. }
  482. int
  483. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  484. int align)
  485. {
  486. int ret;
  487. if (align > obj->base.size)
  488. return -EINVAL;
  489. if (obj->ops == &i915_gem_phys_ops)
  490. return 0;
  491. if (obj->mm.madv != I915_MADV_WILLNEED)
  492. return -EFAULT;
  493. if (obj->base.filp == NULL)
  494. return -EINVAL;
  495. ret = i915_gem_object_unbind(obj);
  496. if (ret)
  497. return ret;
  498. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  499. if (obj->mm.pages)
  500. return -EBUSY;
  501. GEM_BUG_ON(obj->ops != &i915_gem_object_ops);
  502. obj->ops = &i915_gem_phys_ops;
  503. ret = i915_gem_object_pin_pages(obj);
  504. if (ret)
  505. goto err_xfer;
  506. return 0;
  507. err_xfer:
  508. obj->ops = &i915_gem_object_ops;
  509. return ret;
  510. }
  511. static int
  512. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  513. struct drm_i915_gem_pwrite *args,
  514. struct drm_file *file)
  515. {
  516. void *vaddr = obj->phys_handle->vaddr + args->offset;
  517. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  518. /* We manually control the domain here and pretend that it
  519. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  520. */
  521. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  522. if (copy_from_user(vaddr, user_data, args->size))
  523. return -EFAULT;
  524. drm_clflush_virt_range(vaddr, args->size);
  525. i915_gem_chipset_flush(to_i915(obj->base.dev));
  526. intel_fb_obj_flush(obj, ORIGIN_CPU);
  527. return 0;
  528. }
  529. void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
  530. {
  531. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  532. }
  533. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  534. {
  535. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  536. kmem_cache_free(dev_priv->objects, obj);
  537. }
  538. static int
  539. i915_gem_create(struct drm_file *file,
  540. struct drm_i915_private *dev_priv,
  541. uint64_t size,
  542. uint32_t *handle_p)
  543. {
  544. struct drm_i915_gem_object *obj;
  545. int ret;
  546. u32 handle;
  547. size = roundup(size, PAGE_SIZE);
  548. if (size == 0)
  549. return -EINVAL;
  550. /* Allocate the new object */
  551. obj = i915_gem_object_create(dev_priv, size);
  552. if (IS_ERR(obj))
  553. return PTR_ERR(obj);
  554. ret = drm_gem_handle_create(file, &obj->base, &handle);
  555. /* drop reference from allocate - handle holds it now */
  556. i915_gem_object_put(obj);
  557. if (ret)
  558. return ret;
  559. *handle_p = handle;
  560. return 0;
  561. }
  562. int
  563. i915_gem_dumb_create(struct drm_file *file,
  564. struct drm_device *dev,
  565. struct drm_mode_create_dumb *args)
  566. {
  567. /* have to work out size/pitch and return them */
  568. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  569. args->size = args->pitch * args->height;
  570. return i915_gem_create(file, to_i915(dev),
  571. args->size, &args->handle);
  572. }
  573. /**
  574. * Creates a new mm object and returns a handle to it.
  575. * @dev: drm device pointer
  576. * @data: ioctl data blob
  577. * @file: drm file pointer
  578. */
  579. int
  580. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  581. struct drm_file *file)
  582. {
  583. struct drm_i915_private *dev_priv = to_i915(dev);
  584. struct drm_i915_gem_create *args = data;
  585. i915_gem_flush_free_objects(dev_priv);
  586. return i915_gem_create(file, dev_priv,
  587. args->size, &args->handle);
  588. }
  589. static inline int
  590. __copy_to_user_swizzled(char __user *cpu_vaddr,
  591. const char *gpu_vaddr, int gpu_offset,
  592. int length)
  593. {
  594. int ret, cpu_offset = 0;
  595. while (length > 0) {
  596. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  597. int this_length = min(cacheline_end - gpu_offset, length);
  598. int swizzled_gpu_offset = gpu_offset ^ 64;
  599. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  600. gpu_vaddr + swizzled_gpu_offset,
  601. this_length);
  602. if (ret)
  603. return ret + length;
  604. cpu_offset += this_length;
  605. gpu_offset += this_length;
  606. length -= this_length;
  607. }
  608. return 0;
  609. }
  610. static inline int
  611. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  612. const char __user *cpu_vaddr,
  613. int length)
  614. {
  615. int ret, cpu_offset = 0;
  616. while (length > 0) {
  617. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  618. int this_length = min(cacheline_end - gpu_offset, length);
  619. int swizzled_gpu_offset = gpu_offset ^ 64;
  620. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  621. cpu_vaddr + cpu_offset,
  622. this_length);
  623. if (ret)
  624. return ret + length;
  625. cpu_offset += this_length;
  626. gpu_offset += this_length;
  627. length -= this_length;
  628. }
  629. return 0;
  630. }
  631. /*
  632. * Pins the specified object's pages and synchronizes the object with
  633. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  634. * flush the object from the CPU cache.
  635. */
  636. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  637. unsigned int *needs_clflush)
  638. {
  639. int ret;
  640. lockdep_assert_held(&obj->base.dev->struct_mutex);
  641. *needs_clflush = 0;
  642. if (!i915_gem_object_has_struct_page(obj))
  643. return -ENODEV;
  644. ret = i915_gem_object_wait(obj,
  645. I915_WAIT_INTERRUPTIBLE |
  646. I915_WAIT_LOCKED,
  647. MAX_SCHEDULE_TIMEOUT,
  648. NULL);
  649. if (ret)
  650. return ret;
  651. ret = i915_gem_object_pin_pages(obj);
  652. if (ret)
  653. return ret;
  654. if (i915_gem_object_is_coherent(obj) ||
  655. !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  656. ret = i915_gem_object_set_to_cpu_domain(obj, false);
  657. if (ret)
  658. goto err_unpin;
  659. else
  660. goto out;
  661. }
  662. i915_gem_object_flush_gtt_write_domain(obj);
  663. /* If we're not in the cpu read domain, set ourself into the gtt
  664. * read domain and manually flush cachelines (if required). This
  665. * optimizes for the case when the gpu will dirty the data
  666. * anyway again before the next pread happens.
  667. */
  668. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  669. *needs_clflush = CLFLUSH_BEFORE;
  670. out:
  671. /* return with the pages pinned */
  672. return 0;
  673. err_unpin:
  674. i915_gem_object_unpin_pages(obj);
  675. return ret;
  676. }
  677. int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
  678. unsigned int *needs_clflush)
  679. {
  680. int ret;
  681. lockdep_assert_held(&obj->base.dev->struct_mutex);
  682. *needs_clflush = 0;
  683. if (!i915_gem_object_has_struct_page(obj))
  684. return -ENODEV;
  685. ret = i915_gem_object_wait(obj,
  686. I915_WAIT_INTERRUPTIBLE |
  687. I915_WAIT_LOCKED |
  688. I915_WAIT_ALL,
  689. MAX_SCHEDULE_TIMEOUT,
  690. NULL);
  691. if (ret)
  692. return ret;
  693. ret = i915_gem_object_pin_pages(obj);
  694. if (ret)
  695. return ret;
  696. if (i915_gem_object_is_coherent(obj) ||
  697. !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  698. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  699. if (ret)
  700. goto err_unpin;
  701. else
  702. goto out;
  703. }
  704. i915_gem_object_flush_gtt_write_domain(obj);
  705. /* If we're not in the cpu write domain, set ourself into the
  706. * gtt write domain and manually flush cachelines (as required).
  707. * This optimizes for the case when the gpu will use the data
  708. * right away and we therefore have to clflush anyway.
  709. */
  710. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  711. *needs_clflush |= CLFLUSH_AFTER;
  712. /* Same trick applies to invalidate partially written cachelines read
  713. * before writing.
  714. */
  715. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  716. *needs_clflush |= CLFLUSH_BEFORE;
  717. out:
  718. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  719. obj->mm.dirty = true;
  720. /* return with the pages pinned */
  721. return 0;
  722. err_unpin:
  723. i915_gem_object_unpin_pages(obj);
  724. return ret;
  725. }
  726. static void
  727. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  728. bool swizzled)
  729. {
  730. if (unlikely(swizzled)) {
  731. unsigned long start = (unsigned long) addr;
  732. unsigned long end = (unsigned long) addr + length;
  733. /* For swizzling simply ensure that we always flush both
  734. * channels. Lame, but simple and it works. Swizzled
  735. * pwrite/pread is far from a hotpath - current userspace
  736. * doesn't use it at all. */
  737. start = round_down(start, 128);
  738. end = round_up(end, 128);
  739. drm_clflush_virt_range((void *)start, end - start);
  740. } else {
  741. drm_clflush_virt_range(addr, length);
  742. }
  743. }
  744. /* Only difference to the fast-path function is that this can handle bit17
  745. * and uses non-atomic copy and kmap functions. */
  746. static int
  747. shmem_pread_slow(struct page *page, int offset, int length,
  748. char __user *user_data,
  749. bool page_do_bit17_swizzling, bool needs_clflush)
  750. {
  751. char *vaddr;
  752. int ret;
  753. vaddr = kmap(page);
  754. if (needs_clflush)
  755. shmem_clflush_swizzled_range(vaddr + offset, length,
  756. page_do_bit17_swizzling);
  757. if (page_do_bit17_swizzling)
  758. ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
  759. else
  760. ret = __copy_to_user(user_data, vaddr + offset, length);
  761. kunmap(page);
  762. return ret ? - EFAULT : 0;
  763. }
  764. static int
  765. shmem_pread(struct page *page, int offset, int length, char __user *user_data,
  766. bool page_do_bit17_swizzling, bool needs_clflush)
  767. {
  768. int ret;
  769. ret = -ENODEV;
  770. if (!page_do_bit17_swizzling) {
  771. char *vaddr = kmap_atomic(page);
  772. if (needs_clflush)
  773. drm_clflush_virt_range(vaddr + offset, length);
  774. ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
  775. kunmap_atomic(vaddr);
  776. }
  777. if (ret == 0)
  778. return 0;
  779. return shmem_pread_slow(page, offset, length, user_data,
  780. page_do_bit17_swizzling, needs_clflush);
  781. }
  782. static int
  783. i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
  784. struct drm_i915_gem_pread *args)
  785. {
  786. char __user *user_data;
  787. u64 remain;
  788. unsigned int obj_do_bit17_swizzling;
  789. unsigned int needs_clflush;
  790. unsigned int idx, offset;
  791. int ret;
  792. obj_do_bit17_swizzling = 0;
  793. if (i915_gem_object_needs_bit17_swizzle(obj))
  794. obj_do_bit17_swizzling = BIT(17);
  795. ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
  796. if (ret)
  797. return ret;
  798. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  799. mutex_unlock(&obj->base.dev->struct_mutex);
  800. if (ret)
  801. return ret;
  802. remain = args->size;
  803. user_data = u64_to_user_ptr(args->data_ptr);
  804. offset = offset_in_page(args->offset);
  805. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  806. struct page *page = i915_gem_object_get_page(obj, idx);
  807. int length;
  808. length = remain;
  809. if (offset + length > PAGE_SIZE)
  810. length = PAGE_SIZE - offset;
  811. ret = shmem_pread(page, offset, length, user_data,
  812. page_to_phys(page) & obj_do_bit17_swizzling,
  813. needs_clflush);
  814. if (ret)
  815. break;
  816. remain -= length;
  817. user_data += length;
  818. offset = 0;
  819. }
  820. i915_gem_obj_finish_shmem_access(obj);
  821. return ret;
  822. }
  823. static inline bool
  824. gtt_user_read(struct io_mapping *mapping,
  825. loff_t base, int offset,
  826. char __user *user_data, int length)
  827. {
  828. void *vaddr;
  829. unsigned long unwritten;
  830. /* We can use the cpu mem copy function because this is X86. */
  831. vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
  832. unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length);
  833. io_mapping_unmap_atomic(vaddr);
  834. if (unwritten) {
  835. vaddr = (void __force *)
  836. io_mapping_map_wc(mapping, base, PAGE_SIZE);
  837. unwritten = copy_to_user(user_data, vaddr + offset, length);
  838. io_mapping_unmap(vaddr);
  839. }
  840. return unwritten;
  841. }
  842. static int
  843. i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
  844. const struct drm_i915_gem_pread *args)
  845. {
  846. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  847. struct i915_ggtt *ggtt = &i915->ggtt;
  848. struct drm_mm_node node;
  849. struct i915_vma *vma;
  850. void __user *user_data;
  851. u64 remain, offset;
  852. int ret;
  853. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  854. if (ret)
  855. return ret;
  856. intel_runtime_pm_get(i915);
  857. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  858. PIN_MAPPABLE | PIN_NONBLOCK);
  859. if (!IS_ERR(vma)) {
  860. node.start = i915_ggtt_offset(vma);
  861. node.allocated = false;
  862. ret = i915_vma_put_fence(vma);
  863. if (ret) {
  864. i915_vma_unpin(vma);
  865. vma = ERR_PTR(ret);
  866. }
  867. }
  868. if (IS_ERR(vma)) {
  869. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  870. if (ret)
  871. goto out_unlock;
  872. GEM_BUG_ON(!node.allocated);
  873. }
  874. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  875. if (ret)
  876. goto out_unpin;
  877. mutex_unlock(&i915->drm.struct_mutex);
  878. user_data = u64_to_user_ptr(args->data_ptr);
  879. remain = args->size;
  880. offset = args->offset;
  881. while (remain > 0) {
  882. /* Operation in this page
  883. *
  884. * page_base = page offset within aperture
  885. * page_offset = offset within page
  886. * page_length = bytes to copy for this page
  887. */
  888. u32 page_base = node.start;
  889. unsigned page_offset = offset_in_page(offset);
  890. unsigned page_length = PAGE_SIZE - page_offset;
  891. page_length = remain < page_length ? remain : page_length;
  892. if (node.allocated) {
  893. wmb();
  894. ggtt->base.insert_page(&ggtt->base,
  895. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  896. node.start, I915_CACHE_NONE, 0);
  897. wmb();
  898. } else {
  899. page_base += offset & PAGE_MASK;
  900. }
  901. if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
  902. user_data, page_length)) {
  903. ret = -EFAULT;
  904. break;
  905. }
  906. remain -= page_length;
  907. user_data += page_length;
  908. offset += page_length;
  909. }
  910. mutex_lock(&i915->drm.struct_mutex);
  911. out_unpin:
  912. if (node.allocated) {
  913. wmb();
  914. ggtt->base.clear_range(&ggtt->base,
  915. node.start, node.size);
  916. remove_mappable_node(&node);
  917. } else {
  918. i915_vma_unpin(vma);
  919. }
  920. out_unlock:
  921. intel_runtime_pm_put(i915);
  922. mutex_unlock(&i915->drm.struct_mutex);
  923. return ret;
  924. }
  925. /**
  926. * Reads data from the object referenced by handle.
  927. * @dev: drm device pointer
  928. * @data: ioctl data blob
  929. * @file: drm file pointer
  930. *
  931. * On error, the contents of *data are undefined.
  932. */
  933. int
  934. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  935. struct drm_file *file)
  936. {
  937. struct drm_i915_gem_pread *args = data;
  938. struct drm_i915_gem_object *obj;
  939. int ret;
  940. if (args->size == 0)
  941. return 0;
  942. if (!access_ok(VERIFY_WRITE,
  943. u64_to_user_ptr(args->data_ptr),
  944. args->size))
  945. return -EFAULT;
  946. obj = i915_gem_object_lookup(file, args->handle);
  947. if (!obj)
  948. return -ENOENT;
  949. /* Bounds check source. */
  950. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  951. ret = -EINVAL;
  952. goto out;
  953. }
  954. trace_i915_gem_object_pread(obj, args->offset, args->size);
  955. ret = i915_gem_object_wait(obj,
  956. I915_WAIT_INTERRUPTIBLE,
  957. MAX_SCHEDULE_TIMEOUT,
  958. to_rps_client(file));
  959. if (ret)
  960. goto out;
  961. ret = i915_gem_object_pin_pages(obj);
  962. if (ret)
  963. goto out;
  964. ret = i915_gem_shmem_pread(obj, args);
  965. if (ret == -EFAULT || ret == -ENODEV)
  966. ret = i915_gem_gtt_pread(obj, args);
  967. i915_gem_object_unpin_pages(obj);
  968. out:
  969. i915_gem_object_put(obj);
  970. return ret;
  971. }
  972. /* This is the fast write path which cannot handle
  973. * page faults in the source data
  974. */
  975. static inline bool
  976. ggtt_write(struct io_mapping *mapping,
  977. loff_t base, int offset,
  978. char __user *user_data, int length)
  979. {
  980. void *vaddr;
  981. unsigned long unwritten;
  982. /* We can use the cpu mem copy function because this is X86. */
  983. vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
  984. unwritten = __copy_from_user_inatomic_nocache(vaddr + offset,
  985. user_data, length);
  986. io_mapping_unmap_atomic(vaddr);
  987. if (unwritten) {
  988. vaddr = (void __force *)
  989. io_mapping_map_wc(mapping, base, PAGE_SIZE);
  990. unwritten = copy_from_user(vaddr + offset, user_data, length);
  991. io_mapping_unmap(vaddr);
  992. }
  993. return unwritten;
  994. }
  995. /**
  996. * This is the fast pwrite path, where we copy the data directly from the
  997. * user into the GTT, uncached.
  998. * @obj: i915 GEM object
  999. * @args: pwrite arguments structure
  1000. */
  1001. static int
  1002. i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
  1003. const struct drm_i915_gem_pwrite *args)
  1004. {
  1005. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1006. struct i915_ggtt *ggtt = &i915->ggtt;
  1007. struct drm_mm_node node;
  1008. struct i915_vma *vma;
  1009. u64 remain, offset;
  1010. void __user *user_data;
  1011. int ret;
  1012. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1013. if (ret)
  1014. return ret;
  1015. intel_runtime_pm_get(i915);
  1016. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  1017. PIN_MAPPABLE | PIN_NONBLOCK);
  1018. if (!IS_ERR(vma)) {
  1019. node.start = i915_ggtt_offset(vma);
  1020. node.allocated = false;
  1021. ret = i915_vma_put_fence(vma);
  1022. if (ret) {
  1023. i915_vma_unpin(vma);
  1024. vma = ERR_PTR(ret);
  1025. }
  1026. }
  1027. if (IS_ERR(vma)) {
  1028. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  1029. if (ret)
  1030. goto out_unlock;
  1031. GEM_BUG_ON(!node.allocated);
  1032. }
  1033. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  1034. if (ret)
  1035. goto out_unpin;
  1036. mutex_unlock(&i915->drm.struct_mutex);
  1037. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  1038. user_data = u64_to_user_ptr(args->data_ptr);
  1039. offset = args->offset;
  1040. remain = args->size;
  1041. while (remain) {
  1042. /* Operation in this page
  1043. *
  1044. * page_base = page offset within aperture
  1045. * page_offset = offset within page
  1046. * page_length = bytes to copy for this page
  1047. */
  1048. u32 page_base = node.start;
  1049. unsigned int page_offset = offset_in_page(offset);
  1050. unsigned int page_length = PAGE_SIZE - page_offset;
  1051. page_length = remain < page_length ? remain : page_length;
  1052. if (node.allocated) {
  1053. wmb(); /* flush the write before we modify the GGTT */
  1054. ggtt->base.insert_page(&ggtt->base,
  1055. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  1056. node.start, I915_CACHE_NONE, 0);
  1057. wmb(); /* flush modifications to the GGTT (insert_page) */
  1058. } else {
  1059. page_base += offset & PAGE_MASK;
  1060. }
  1061. /* If we get a fault while copying data, then (presumably) our
  1062. * source page isn't available. Return the error and we'll
  1063. * retry in the slow path.
  1064. * If the object is non-shmem backed, we retry again with the
  1065. * path that handles page fault.
  1066. */
  1067. if (ggtt_write(&ggtt->mappable, page_base, page_offset,
  1068. user_data, page_length)) {
  1069. ret = -EFAULT;
  1070. break;
  1071. }
  1072. remain -= page_length;
  1073. user_data += page_length;
  1074. offset += page_length;
  1075. }
  1076. intel_fb_obj_flush(obj, ORIGIN_CPU);
  1077. mutex_lock(&i915->drm.struct_mutex);
  1078. out_unpin:
  1079. if (node.allocated) {
  1080. wmb();
  1081. ggtt->base.clear_range(&ggtt->base,
  1082. node.start, node.size);
  1083. remove_mappable_node(&node);
  1084. } else {
  1085. i915_vma_unpin(vma);
  1086. }
  1087. out_unlock:
  1088. intel_runtime_pm_put(i915);
  1089. mutex_unlock(&i915->drm.struct_mutex);
  1090. return ret;
  1091. }
  1092. static int
  1093. shmem_pwrite_slow(struct page *page, int offset, int length,
  1094. char __user *user_data,
  1095. bool page_do_bit17_swizzling,
  1096. bool needs_clflush_before,
  1097. bool needs_clflush_after)
  1098. {
  1099. char *vaddr;
  1100. int ret;
  1101. vaddr = kmap(page);
  1102. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  1103. shmem_clflush_swizzled_range(vaddr + offset, length,
  1104. page_do_bit17_swizzling);
  1105. if (page_do_bit17_swizzling)
  1106. ret = __copy_from_user_swizzled(vaddr, offset, user_data,
  1107. length);
  1108. else
  1109. ret = __copy_from_user(vaddr + offset, user_data, length);
  1110. if (needs_clflush_after)
  1111. shmem_clflush_swizzled_range(vaddr + offset, length,
  1112. page_do_bit17_swizzling);
  1113. kunmap(page);
  1114. return ret ? -EFAULT : 0;
  1115. }
  1116. /* Per-page copy function for the shmem pwrite fastpath.
  1117. * Flushes invalid cachelines before writing to the target if
  1118. * needs_clflush_before is set and flushes out any written cachelines after
  1119. * writing if needs_clflush is set.
  1120. */
  1121. static int
  1122. shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
  1123. bool page_do_bit17_swizzling,
  1124. bool needs_clflush_before,
  1125. bool needs_clflush_after)
  1126. {
  1127. int ret;
  1128. ret = -ENODEV;
  1129. if (!page_do_bit17_swizzling) {
  1130. char *vaddr = kmap_atomic(page);
  1131. if (needs_clflush_before)
  1132. drm_clflush_virt_range(vaddr + offset, len);
  1133. ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
  1134. if (needs_clflush_after)
  1135. drm_clflush_virt_range(vaddr + offset, len);
  1136. kunmap_atomic(vaddr);
  1137. }
  1138. if (ret == 0)
  1139. return ret;
  1140. return shmem_pwrite_slow(page, offset, len, user_data,
  1141. page_do_bit17_swizzling,
  1142. needs_clflush_before,
  1143. needs_clflush_after);
  1144. }
  1145. static int
  1146. i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
  1147. const struct drm_i915_gem_pwrite *args)
  1148. {
  1149. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1150. void __user *user_data;
  1151. u64 remain;
  1152. unsigned int obj_do_bit17_swizzling;
  1153. unsigned int partial_cacheline_write;
  1154. unsigned int needs_clflush;
  1155. unsigned int offset, idx;
  1156. int ret;
  1157. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1158. if (ret)
  1159. return ret;
  1160. ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
  1161. mutex_unlock(&i915->drm.struct_mutex);
  1162. if (ret)
  1163. return ret;
  1164. obj_do_bit17_swizzling = 0;
  1165. if (i915_gem_object_needs_bit17_swizzle(obj))
  1166. obj_do_bit17_swizzling = BIT(17);
  1167. /* If we don't overwrite a cacheline completely we need to be
  1168. * careful to have up-to-date data by first clflushing. Don't
  1169. * overcomplicate things and flush the entire patch.
  1170. */
  1171. partial_cacheline_write = 0;
  1172. if (needs_clflush & CLFLUSH_BEFORE)
  1173. partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
  1174. user_data = u64_to_user_ptr(args->data_ptr);
  1175. remain = args->size;
  1176. offset = offset_in_page(args->offset);
  1177. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  1178. struct page *page = i915_gem_object_get_page(obj, idx);
  1179. int length;
  1180. length = remain;
  1181. if (offset + length > PAGE_SIZE)
  1182. length = PAGE_SIZE - offset;
  1183. ret = shmem_pwrite(page, offset, length, user_data,
  1184. page_to_phys(page) & obj_do_bit17_swizzling,
  1185. (offset | length) & partial_cacheline_write,
  1186. needs_clflush & CLFLUSH_AFTER);
  1187. if (ret)
  1188. break;
  1189. remain -= length;
  1190. user_data += length;
  1191. offset = 0;
  1192. }
  1193. intel_fb_obj_flush(obj, ORIGIN_CPU);
  1194. i915_gem_obj_finish_shmem_access(obj);
  1195. return ret;
  1196. }
  1197. /**
  1198. * Writes data to the object referenced by handle.
  1199. * @dev: drm device
  1200. * @data: ioctl data blob
  1201. * @file: drm file
  1202. *
  1203. * On error, the contents of the buffer that were to be modified are undefined.
  1204. */
  1205. int
  1206. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  1207. struct drm_file *file)
  1208. {
  1209. struct drm_i915_gem_pwrite *args = data;
  1210. struct drm_i915_gem_object *obj;
  1211. int ret;
  1212. if (args->size == 0)
  1213. return 0;
  1214. if (!access_ok(VERIFY_READ,
  1215. u64_to_user_ptr(args->data_ptr),
  1216. args->size))
  1217. return -EFAULT;
  1218. obj = i915_gem_object_lookup(file, args->handle);
  1219. if (!obj)
  1220. return -ENOENT;
  1221. /* Bounds check destination. */
  1222. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  1223. ret = -EINVAL;
  1224. goto err;
  1225. }
  1226. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  1227. ret = -ENODEV;
  1228. if (obj->ops->pwrite)
  1229. ret = obj->ops->pwrite(obj, args);
  1230. if (ret != -ENODEV)
  1231. goto err;
  1232. ret = i915_gem_object_wait(obj,
  1233. I915_WAIT_INTERRUPTIBLE |
  1234. I915_WAIT_ALL,
  1235. MAX_SCHEDULE_TIMEOUT,
  1236. to_rps_client(file));
  1237. if (ret)
  1238. goto err;
  1239. ret = i915_gem_object_pin_pages(obj);
  1240. if (ret)
  1241. goto err;
  1242. ret = -EFAULT;
  1243. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  1244. * it would end up going through the fenced access, and we'll get
  1245. * different detiling behavior between reading and writing.
  1246. * pread/pwrite currently are reading and writing from the CPU
  1247. * perspective, requiring manual detiling by the client.
  1248. */
  1249. if (!i915_gem_object_has_struct_page(obj) ||
  1250. cpu_write_needs_clflush(obj))
  1251. /* Note that the gtt paths might fail with non-page-backed user
  1252. * pointers (e.g. gtt mappings when moving data between
  1253. * textures). Fallback to the shmem path in that case.
  1254. */
  1255. ret = i915_gem_gtt_pwrite_fast(obj, args);
  1256. if (ret == -EFAULT || ret == -ENOSPC) {
  1257. if (obj->phys_handle)
  1258. ret = i915_gem_phys_pwrite(obj, args, file);
  1259. else
  1260. ret = i915_gem_shmem_pwrite(obj, args);
  1261. }
  1262. i915_gem_object_unpin_pages(obj);
  1263. err:
  1264. i915_gem_object_put(obj);
  1265. return ret;
  1266. }
  1267. static inline enum fb_op_origin
  1268. write_origin(struct drm_i915_gem_object *obj, unsigned domain)
  1269. {
  1270. return (domain == I915_GEM_DOMAIN_GTT ?
  1271. obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
  1272. }
  1273. static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
  1274. {
  1275. struct drm_i915_private *i915;
  1276. struct list_head *list;
  1277. struct i915_vma *vma;
  1278. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  1279. if (!i915_vma_is_ggtt(vma))
  1280. break;
  1281. if (i915_vma_is_active(vma))
  1282. continue;
  1283. if (!drm_mm_node_allocated(&vma->node))
  1284. continue;
  1285. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  1286. }
  1287. i915 = to_i915(obj->base.dev);
  1288. list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
  1289. list_move_tail(&obj->global_link, list);
  1290. }
  1291. /**
  1292. * Called when user space prepares to use an object with the CPU, either
  1293. * through the mmap ioctl's mapping or a GTT mapping.
  1294. * @dev: drm device
  1295. * @data: ioctl data blob
  1296. * @file: drm file
  1297. */
  1298. int
  1299. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1300. struct drm_file *file)
  1301. {
  1302. struct drm_i915_gem_set_domain *args = data;
  1303. struct drm_i915_gem_object *obj;
  1304. uint32_t read_domains = args->read_domains;
  1305. uint32_t write_domain = args->write_domain;
  1306. int err;
  1307. /* Only handle setting domains to types used by the CPU. */
  1308. if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
  1309. return -EINVAL;
  1310. /* Having something in the write domain implies it's in the read
  1311. * domain, and only that read domain. Enforce that in the request.
  1312. */
  1313. if (write_domain != 0 && read_domains != write_domain)
  1314. return -EINVAL;
  1315. obj = i915_gem_object_lookup(file, args->handle);
  1316. if (!obj)
  1317. return -ENOENT;
  1318. /* Try to flush the object off the GPU without holding the lock.
  1319. * We will repeat the flush holding the lock in the normal manner
  1320. * to catch cases where we are gazumped.
  1321. */
  1322. err = i915_gem_object_wait(obj,
  1323. I915_WAIT_INTERRUPTIBLE |
  1324. (write_domain ? I915_WAIT_ALL : 0),
  1325. MAX_SCHEDULE_TIMEOUT,
  1326. to_rps_client(file));
  1327. if (err)
  1328. goto out;
  1329. /* Flush and acquire obj->pages so that we are coherent through
  1330. * direct access in memory with previous cached writes through
  1331. * shmemfs and that our cache domain tracking remains valid.
  1332. * For example, if the obj->filp was moved to swap without us
  1333. * being notified and releasing the pages, we would mistakenly
  1334. * continue to assume that the obj remained out of the CPU cached
  1335. * domain.
  1336. */
  1337. err = i915_gem_object_pin_pages(obj);
  1338. if (err)
  1339. goto out;
  1340. err = i915_mutex_lock_interruptible(dev);
  1341. if (err)
  1342. goto out_unpin;
  1343. if (read_domains & I915_GEM_DOMAIN_GTT)
  1344. err = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1345. else
  1346. err = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1347. /* And bump the LRU for this access */
  1348. i915_gem_object_bump_inactive_ggtt(obj);
  1349. mutex_unlock(&dev->struct_mutex);
  1350. if (write_domain != 0)
  1351. intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
  1352. out_unpin:
  1353. i915_gem_object_unpin_pages(obj);
  1354. out:
  1355. i915_gem_object_put(obj);
  1356. return err;
  1357. }
  1358. /**
  1359. * Called when user space has done writes to this buffer
  1360. * @dev: drm device
  1361. * @data: ioctl data blob
  1362. * @file: drm file
  1363. */
  1364. int
  1365. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1366. struct drm_file *file)
  1367. {
  1368. struct drm_i915_gem_sw_finish *args = data;
  1369. struct drm_i915_gem_object *obj;
  1370. obj = i915_gem_object_lookup(file, args->handle);
  1371. if (!obj)
  1372. return -ENOENT;
  1373. /* Pinned buffers may be scanout, so flush the cache */
  1374. i915_gem_object_flush_if_display(obj);
  1375. i915_gem_object_put(obj);
  1376. return 0;
  1377. }
  1378. /**
  1379. * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
  1380. * it is mapped to.
  1381. * @dev: drm device
  1382. * @data: ioctl data blob
  1383. * @file: drm file
  1384. *
  1385. * While the mapping holds a reference on the contents of the object, it doesn't
  1386. * imply a ref on the object itself.
  1387. *
  1388. * IMPORTANT:
  1389. *
  1390. * DRM driver writers who look a this function as an example for how to do GEM
  1391. * mmap support, please don't implement mmap support like here. The modern way
  1392. * to implement DRM mmap support is with an mmap offset ioctl (like
  1393. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1394. * That way debug tooling like valgrind will understand what's going on, hiding
  1395. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1396. * does cpu mmaps this way because we didn't know better.
  1397. */
  1398. int
  1399. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1400. struct drm_file *file)
  1401. {
  1402. struct drm_i915_gem_mmap *args = data;
  1403. struct drm_i915_gem_object *obj;
  1404. unsigned long addr;
  1405. if (args->flags & ~(I915_MMAP_WC))
  1406. return -EINVAL;
  1407. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1408. return -ENODEV;
  1409. obj = i915_gem_object_lookup(file, args->handle);
  1410. if (!obj)
  1411. return -ENOENT;
  1412. /* prime objects have no backing filp to GEM mmap
  1413. * pages from.
  1414. */
  1415. if (!obj->base.filp) {
  1416. i915_gem_object_put(obj);
  1417. return -EINVAL;
  1418. }
  1419. addr = vm_mmap(obj->base.filp, 0, args->size,
  1420. PROT_READ | PROT_WRITE, MAP_SHARED,
  1421. args->offset);
  1422. if (args->flags & I915_MMAP_WC) {
  1423. struct mm_struct *mm = current->mm;
  1424. struct vm_area_struct *vma;
  1425. if (down_write_killable(&mm->mmap_sem)) {
  1426. i915_gem_object_put(obj);
  1427. return -EINTR;
  1428. }
  1429. vma = find_vma(mm, addr);
  1430. if (vma)
  1431. vma->vm_page_prot =
  1432. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1433. else
  1434. addr = -ENOMEM;
  1435. up_write(&mm->mmap_sem);
  1436. /* This may race, but that's ok, it only gets set */
  1437. WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
  1438. }
  1439. i915_gem_object_put(obj);
  1440. if (IS_ERR((void *)addr))
  1441. return addr;
  1442. args->addr_ptr = (uint64_t) addr;
  1443. return 0;
  1444. }
  1445. static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
  1446. {
  1447. return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
  1448. }
  1449. /**
  1450. * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
  1451. *
  1452. * A history of the GTT mmap interface:
  1453. *
  1454. * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
  1455. * aligned and suitable for fencing, and still fit into the available
  1456. * mappable space left by the pinned display objects. A classic problem
  1457. * we called the page-fault-of-doom where we would ping-pong between
  1458. * two objects that could not fit inside the GTT and so the memcpy
  1459. * would page one object in at the expense of the other between every
  1460. * single byte.
  1461. *
  1462. * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
  1463. * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
  1464. * object is too large for the available space (or simply too large
  1465. * for the mappable aperture!), a view is created instead and faulted
  1466. * into userspace. (This view is aligned and sized appropriately for
  1467. * fenced access.)
  1468. *
  1469. * Restrictions:
  1470. *
  1471. * * snoopable objects cannot be accessed via the GTT. It can cause machine
  1472. * hangs on some architectures, corruption on others. An attempt to service
  1473. * a GTT page fault from a snoopable object will generate a SIGBUS.
  1474. *
  1475. * * the object must be able to fit into RAM (physical memory, though no
  1476. * limited to the mappable aperture).
  1477. *
  1478. *
  1479. * Caveats:
  1480. *
  1481. * * a new GTT page fault will synchronize rendering from the GPU and flush
  1482. * all data to system memory. Subsequent access will not be synchronized.
  1483. *
  1484. * * all mappings are revoked on runtime device suspend.
  1485. *
  1486. * * there are only 8, 16 or 32 fence registers to share between all users
  1487. * (older machines require fence register for display and blitter access
  1488. * as well). Contention of the fence registers will cause the previous users
  1489. * to be unmapped and any new access will generate new page faults.
  1490. *
  1491. * * running out of memory while servicing a fault may generate a SIGBUS,
  1492. * rather than the expected SIGSEGV.
  1493. */
  1494. int i915_gem_mmap_gtt_version(void)
  1495. {
  1496. return 1;
  1497. }
  1498. static inline struct i915_ggtt_view
  1499. compute_partial_view(struct drm_i915_gem_object *obj,
  1500. pgoff_t page_offset,
  1501. unsigned int chunk)
  1502. {
  1503. struct i915_ggtt_view view;
  1504. if (i915_gem_object_is_tiled(obj))
  1505. chunk = roundup(chunk, tile_row_pages(obj));
  1506. view.type = I915_GGTT_VIEW_PARTIAL;
  1507. view.partial.offset = rounddown(page_offset, chunk);
  1508. view.partial.size =
  1509. min_t(unsigned int, chunk,
  1510. (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
  1511. /* If the partial covers the entire object, just create a normal VMA. */
  1512. if (chunk >= obj->base.size >> PAGE_SHIFT)
  1513. view.type = I915_GGTT_VIEW_NORMAL;
  1514. return view;
  1515. }
  1516. /**
  1517. * i915_gem_fault - fault a page into the GTT
  1518. * @vmf: fault info
  1519. *
  1520. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1521. * from userspace. The fault handler takes care of binding the object to
  1522. * the GTT (if needed), allocating and programming a fence register (again,
  1523. * only if needed based on whether the old reg is still valid or the object
  1524. * is tiled) and inserting a new PTE into the faulting process.
  1525. *
  1526. * Note that the faulting process may involve evicting existing objects
  1527. * from the GTT and/or fence registers to make room. So performance may
  1528. * suffer if the GTT working set is large or there are few fence registers
  1529. * left.
  1530. *
  1531. * The current feature set supported by i915_gem_fault() and thus GTT mmaps
  1532. * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
  1533. */
  1534. int i915_gem_fault(struct vm_fault *vmf)
  1535. {
  1536. #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
  1537. struct vm_area_struct *area = vmf->vma;
  1538. struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
  1539. struct drm_device *dev = obj->base.dev;
  1540. struct drm_i915_private *dev_priv = to_i915(dev);
  1541. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1542. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1543. struct i915_vma *vma;
  1544. pgoff_t page_offset;
  1545. unsigned int flags;
  1546. int ret;
  1547. /* We don't use vmf->pgoff since that has the fake offset */
  1548. page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
  1549. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1550. /* Try to flush the object off the GPU first without holding the lock.
  1551. * Upon acquiring the lock, we will perform our sanity checks and then
  1552. * repeat the flush holding the lock in the normal manner to catch cases
  1553. * where we are gazumped.
  1554. */
  1555. ret = i915_gem_object_wait(obj,
  1556. I915_WAIT_INTERRUPTIBLE,
  1557. MAX_SCHEDULE_TIMEOUT,
  1558. NULL);
  1559. if (ret)
  1560. goto err;
  1561. ret = i915_gem_object_pin_pages(obj);
  1562. if (ret)
  1563. goto err;
  1564. intel_runtime_pm_get(dev_priv);
  1565. ret = i915_mutex_lock_interruptible(dev);
  1566. if (ret)
  1567. goto err_rpm;
  1568. /* Access to snoopable pages through the GTT is incoherent. */
  1569. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
  1570. ret = -EFAULT;
  1571. goto err_unlock;
  1572. }
  1573. /* If the object is smaller than a couple of partial vma, it is
  1574. * not worth only creating a single partial vma - we may as well
  1575. * clear enough space for the full object.
  1576. */
  1577. flags = PIN_MAPPABLE;
  1578. if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
  1579. flags |= PIN_NONBLOCK | PIN_NONFAULT;
  1580. /* Now pin it into the GTT as needed */
  1581. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
  1582. if (IS_ERR(vma)) {
  1583. /* Use a partial view if it is bigger than available space */
  1584. struct i915_ggtt_view view =
  1585. compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
  1586. /* Userspace is now writing through an untracked VMA, abandon
  1587. * all hope that the hardware is able to track future writes.
  1588. */
  1589. obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
  1590. vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
  1591. }
  1592. if (IS_ERR(vma)) {
  1593. ret = PTR_ERR(vma);
  1594. goto err_unlock;
  1595. }
  1596. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1597. if (ret)
  1598. goto err_unpin;
  1599. ret = i915_vma_get_fence(vma);
  1600. if (ret)
  1601. goto err_unpin;
  1602. /* Mark as being mmapped into userspace for later revocation */
  1603. assert_rpm_wakelock_held(dev_priv);
  1604. if (list_empty(&obj->userfault_link))
  1605. list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
  1606. /* Finally, remap it using the new GTT offset */
  1607. ret = remap_io_mapping(area,
  1608. area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
  1609. (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
  1610. min_t(u64, vma->size, area->vm_end - area->vm_start),
  1611. &ggtt->mappable);
  1612. err_unpin:
  1613. __i915_vma_unpin(vma);
  1614. err_unlock:
  1615. mutex_unlock(&dev->struct_mutex);
  1616. err_rpm:
  1617. intel_runtime_pm_put(dev_priv);
  1618. i915_gem_object_unpin_pages(obj);
  1619. err:
  1620. switch (ret) {
  1621. case -EIO:
  1622. /*
  1623. * We eat errors when the gpu is terminally wedged to avoid
  1624. * userspace unduly crashing (gl has no provisions for mmaps to
  1625. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1626. * and so needs to be reported.
  1627. */
  1628. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1629. ret = VM_FAULT_SIGBUS;
  1630. break;
  1631. }
  1632. case -EAGAIN:
  1633. /*
  1634. * EAGAIN means the gpu is hung and we'll wait for the error
  1635. * handler to reset everything when re-faulting in
  1636. * i915_mutex_lock_interruptible.
  1637. */
  1638. case 0:
  1639. case -ERESTARTSYS:
  1640. case -EINTR:
  1641. case -EBUSY:
  1642. /*
  1643. * EBUSY is ok: this just means that another thread
  1644. * already did the job.
  1645. */
  1646. ret = VM_FAULT_NOPAGE;
  1647. break;
  1648. case -ENOMEM:
  1649. ret = VM_FAULT_OOM;
  1650. break;
  1651. case -ENOSPC:
  1652. case -EFAULT:
  1653. ret = VM_FAULT_SIGBUS;
  1654. break;
  1655. default:
  1656. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1657. ret = VM_FAULT_SIGBUS;
  1658. break;
  1659. }
  1660. return ret;
  1661. }
  1662. /**
  1663. * i915_gem_release_mmap - remove physical page mappings
  1664. * @obj: obj in question
  1665. *
  1666. * Preserve the reservation of the mmapping with the DRM core code, but
  1667. * relinquish ownership of the pages back to the system.
  1668. *
  1669. * It is vital that we remove the page mapping if we have mapped a tiled
  1670. * object through the GTT and then lose the fence register due to
  1671. * resource pressure. Similarly if the object has been moved out of the
  1672. * aperture, than pages mapped into userspace must be revoked. Removing the
  1673. * mapping will then trigger a page fault on the next user access, allowing
  1674. * fixup by i915_gem_fault().
  1675. */
  1676. void
  1677. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1678. {
  1679. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1680. /* Serialisation between user GTT access and our code depends upon
  1681. * revoking the CPU's PTE whilst the mutex is held. The next user
  1682. * pagefault then has to wait until we release the mutex.
  1683. *
  1684. * Note that RPM complicates somewhat by adding an additional
  1685. * requirement that operations to the GGTT be made holding the RPM
  1686. * wakeref.
  1687. */
  1688. lockdep_assert_held(&i915->drm.struct_mutex);
  1689. intel_runtime_pm_get(i915);
  1690. if (list_empty(&obj->userfault_link))
  1691. goto out;
  1692. list_del_init(&obj->userfault_link);
  1693. drm_vma_node_unmap(&obj->base.vma_node,
  1694. obj->base.dev->anon_inode->i_mapping);
  1695. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1696. * memory transactions from userspace before we return. The TLB
  1697. * flushing implied above by changing the PTE above *should* be
  1698. * sufficient, an extra barrier here just provides us with a bit
  1699. * of paranoid documentation about our requirement to serialise
  1700. * memory writes before touching registers / GSM.
  1701. */
  1702. wmb();
  1703. out:
  1704. intel_runtime_pm_put(i915);
  1705. }
  1706. void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
  1707. {
  1708. struct drm_i915_gem_object *obj, *on;
  1709. int i;
  1710. /*
  1711. * Only called during RPM suspend. All users of the userfault_list
  1712. * must be holding an RPM wakeref to ensure that this can not
  1713. * run concurrently with themselves (and use the struct_mutex for
  1714. * protection between themselves).
  1715. */
  1716. list_for_each_entry_safe(obj, on,
  1717. &dev_priv->mm.userfault_list, userfault_link) {
  1718. list_del_init(&obj->userfault_link);
  1719. drm_vma_node_unmap(&obj->base.vma_node,
  1720. obj->base.dev->anon_inode->i_mapping);
  1721. }
  1722. /* The fence will be lost when the device powers down. If any were
  1723. * in use by hardware (i.e. they are pinned), we should not be powering
  1724. * down! All other fences will be reacquired by the user upon waking.
  1725. */
  1726. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1727. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1728. /* Ideally we want to assert that the fence register is not
  1729. * live at this point (i.e. that no piece of code will be
  1730. * trying to write through fence + GTT, as that both violates
  1731. * our tracking of activity and associated locking/barriers,
  1732. * but also is illegal given that the hw is powered down).
  1733. *
  1734. * Previously we used reg->pin_count as a "liveness" indicator.
  1735. * That is not sufficient, and we need a more fine-grained
  1736. * tool if we want to have a sanity check here.
  1737. */
  1738. if (!reg->vma)
  1739. continue;
  1740. GEM_BUG_ON(!list_empty(&reg->vma->obj->userfault_link));
  1741. reg->dirty = true;
  1742. }
  1743. }
  1744. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1745. {
  1746. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1747. int err;
  1748. err = drm_gem_create_mmap_offset(&obj->base);
  1749. if (likely(!err))
  1750. return 0;
  1751. /* Attempt to reap some mmap space from dead objects */
  1752. do {
  1753. err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
  1754. if (err)
  1755. break;
  1756. i915_gem_drain_freed_objects(dev_priv);
  1757. err = drm_gem_create_mmap_offset(&obj->base);
  1758. if (!err)
  1759. break;
  1760. } while (flush_delayed_work(&dev_priv->gt.retire_work));
  1761. return err;
  1762. }
  1763. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1764. {
  1765. drm_gem_free_mmap_offset(&obj->base);
  1766. }
  1767. int
  1768. i915_gem_mmap_gtt(struct drm_file *file,
  1769. struct drm_device *dev,
  1770. uint32_t handle,
  1771. uint64_t *offset)
  1772. {
  1773. struct drm_i915_gem_object *obj;
  1774. int ret;
  1775. obj = i915_gem_object_lookup(file, handle);
  1776. if (!obj)
  1777. return -ENOENT;
  1778. ret = i915_gem_object_create_mmap_offset(obj);
  1779. if (ret == 0)
  1780. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1781. i915_gem_object_put(obj);
  1782. return ret;
  1783. }
  1784. /**
  1785. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1786. * @dev: DRM device
  1787. * @data: GTT mapping ioctl data
  1788. * @file: GEM object info
  1789. *
  1790. * Simply returns the fake offset to userspace so it can mmap it.
  1791. * The mmap call will end up in drm_gem_mmap(), which will set things
  1792. * up so we can get faults in the handler above.
  1793. *
  1794. * The fault handler will take care of binding the object into the GTT
  1795. * (since it may have been evicted to make room for something), allocating
  1796. * a fence register, and mapping the appropriate aperture address into
  1797. * userspace.
  1798. */
  1799. int
  1800. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1801. struct drm_file *file)
  1802. {
  1803. struct drm_i915_gem_mmap_gtt *args = data;
  1804. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1805. }
  1806. /* Immediately discard the backing storage */
  1807. static void
  1808. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1809. {
  1810. i915_gem_object_free_mmap_offset(obj);
  1811. if (obj->base.filp == NULL)
  1812. return;
  1813. /* Our goal here is to return as much of the memory as
  1814. * is possible back to the system as we are called from OOM.
  1815. * To do this we must instruct the shmfs to drop all of its
  1816. * backing pages, *now*.
  1817. */
  1818. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1819. obj->mm.madv = __I915_MADV_PURGED;
  1820. obj->mm.pages = ERR_PTR(-EFAULT);
  1821. }
  1822. /* Try to discard unwanted pages */
  1823. void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1824. {
  1825. struct address_space *mapping;
  1826. lockdep_assert_held(&obj->mm.lock);
  1827. GEM_BUG_ON(obj->mm.pages);
  1828. switch (obj->mm.madv) {
  1829. case I915_MADV_DONTNEED:
  1830. i915_gem_object_truncate(obj);
  1831. case __I915_MADV_PURGED:
  1832. return;
  1833. }
  1834. if (obj->base.filp == NULL)
  1835. return;
  1836. mapping = obj->base.filp->f_mapping,
  1837. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1838. }
  1839. static void
  1840. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
  1841. struct sg_table *pages)
  1842. {
  1843. struct sgt_iter sgt_iter;
  1844. struct page *page;
  1845. __i915_gem_object_release_shmem(obj, pages, true);
  1846. i915_gem_gtt_finish_pages(obj, pages);
  1847. if (i915_gem_object_needs_bit17_swizzle(obj))
  1848. i915_gem_object_save_bit_17_swizzle(obj, pages);
  1849. for_each_sgt_page(page, sgt_iter, pages) {
  1850. if (obj->mm.dirty)
  1851. set_page_dirty(page);
  1852. if (obj->mm.madv == I915_MADV_WILLNEED)
  1853. mark_page_accessed(page);
  1854. put_page(page);
  1855. }
  1856. obj->mm.dirty = false;
  1857. sg_free_table(pages);
  1858. kfree(pages);
  1859. }
  1860. static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
  1861. {
  1862. struct radix_tree_iter iter;
  1863. void **slot;
  1864. radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
  1865. radix_tree_delete(&obj->mm.get_page.radix, iter.index);
  1866. }
  1867. void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
  1868. enum i915_mm_subclass subclass)
  1869. {
  1870. struct sg_table *pages;
  1871. if (i915_gem_object_has_pinned_pages(obj))
  1872. return;
  1873. GEM_BUG_ON(obj->bind_count);
  1874. if (!READ_ONCE(obj->mm.pages))
  1875. return;
  1876. /* May be called by shrinker from within get_pages() (on another bo) */
  1877. mutex_lock_nested(&obj->mm.lock, subclass);
  1878. if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
  1879. goto unlock;
  1880. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1881. * array, hence protect them from being reaped by removing them from gtt
  1882. * lists early. */
  1883. pages = fetch_and_zero(&obj->mm.pages);
  1884. GEM_BUG_ON(!pages);
  1885. if (obj->mm.mapping) {
  1886. void *ptr;
  1887. ptr = ptr_mask_bits(obj->mm.mapping);
  1888. if (is_vmalloc_addr(ptr))
  1889. vunmap(ptr);
  1890. else
  1891. kunmap(kmap_to_page(ptr));
  1892. obj->mm.mapping = NULL;
  1893. }
  1894. __i915_gem_object_reset_page_iter(obj);
  1895. if (!IS_ERR(pages))
  1896. obj->ops->put_pages(obj, pages);
  1897. unlock:
  1898. mutex_unlock(&obj->mm.lock);
  1899. }
  1900. static bool i915_sg_trim(struct sg_table *orig_st)
  1901. {
  1902. struct sg_table new_st;
  1903. struct scatterlist *sg, *new_sg;
  1904. unsigned int i;
  1905. if (orig_st->nents == orig_st->orig_nents)
  1906. return false;
  1907. if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
  1908. return false;
  1909. new_sg = new_st.sgl;
  1910. for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
  1911. sg_set_page(new_sg, sg_page(sg), sg->length, 0);
  1912. /* called before being DMA mapped, no need to copy sg->dma_* */
  1913. new_sg = sg_next(new_sg);
  1914. }
  1915. GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
  1916. sg_free_table(orig_st);
  1917. *orig_st = new_st;
  1918. return true;
  1919. }
  1920. static struct sg_table *
  1921. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1922. {
  1923. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1924. const unsigned long page_count = obj->base.size / PAGE_SIZE;
  1925. unsigned long i;
  1926. struct address_space *mapping;
  1927. struct sg_table *st;
  1928. struct scatterlist *sg;
  1929. struct sgt_iter sgt_iter;
  1930. struct page *page;
  1931. unsigned long last_pfn = 0; /* suppress gcc warning */
  1932. unsigned int max_segment;
  1933. int ret;
  1934. gfp_t gfp;
  1935. /* Assert that the object is not currently in any GPU domain. As it
  1936. * wasn't in the GTT, there shouldn't be any way it could have been in
  1937. * a GPU cache
  1938. */
  1939. GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1940. GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1941. max_segment = swiotlb_max_segment();
  1942. if (!max_segment)
  1943. max_segment = rounddown(UINT_MAX, PAGE_SIZE);
  1944. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1945. if (st == NULL)
  1946. return ERR_PTR(-ENOMEM);
  1947. rebuild_st:
  1948. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1949. kfree(st);
  1950. return ERR_PTR(-ENOMEM);
  1951. }
  1952. /* Get the list of pages out of our struct file. They'll be pinned
  1953. * at this point until we release them.
  1954. *
  1955. * Fail silently without starting the shrinker
  1956. */
  1957. mapping = obj->base.filp->f_mapping;
  1958. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  1959. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  1960. sg = st->sgl;
  1961. st->nents = 0;
  1962. for (i = 0; i < page_count; i++) {
  1963. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1964. if (unlikely(IS_ERR(page))) {
  1965. i915_gem_shrink(dev_priv,
  1966. page_count,
  1967. I915_SHRINK_BOUND |
  1968. I915_SHRINK_UNBOUND |
  1969. I915_SHRINK_PURGEABLE);
  1970. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1971. }
  1972. if (unlikely(IS_ERR(page))) {
  1973. gfp_t reclaim;
  1974. /* We've tried hard to allocate the memory by reaping
  1975. * our own buffer, now let the real VM do its job and
  1976. * go down in flames if truly OOM.
  1977. *
  1978. * However, since graphics tend to be disposable,
  1979. * defer the oom here by reporting the ENOMEM back
  1980. * to userspace.
  1981. */
  1982. reclaim = mapping_gfp_mask(mapping);
  1983. reclaim |= __GFP_NORETRY; /* reclaim, but no oom */
  1984. page = shmem_read_mapping_page_gfp(mapping, i, reclaim);
  1985. if (IS_ERR(page)) {
  1986. ret = PTR_ERR(page);
  1987. goto err_sg;
  1988. }
  1989. }
  1990. if (!i ||
  1991. sg->length >= max_segment ||
  1992. page_to_pfn(page) != last_pfn + 1) {
  1993. if (i)
  1994. sg = sg_next(sg);
  1995. st->nents++;
  1996. sg_set_page(sg, page, PAGE_SIZE, 0);
  1997. } else {
  1998. sg->length += PAGE_SIZE;
  1999. }
  2000. last_pfn = page_to_pfn(page);
  2001. /* Check that the i965g/gm workaround works. */
  2002. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  2003. }
  2004. if (sg) /* loop terminated early; short sg table */
  2005. sg_mark_end(sg);
  2006. /* Trim unused sg entries to avoid wasting memory. */
  2007. i915_sg_trim(st);
  2008. ret = i915_gem_gtt_prepare_pages(obj, st);
  2009. if (ret) {
  2010. /* DMA remapping failed? One possible cause is that
  2011. * it could not reserve enough large entries, asking
  2012. * for PAGE_SIZE chunks instead may be helpful.
  2013. */
  2014. if (max_segment > PAGE_SIZE) {
  2015. for_each_sgt_page(page, sgt_iter, st)
  2016. put_page(page);
  2017. sg_free_table(st);
  2018. max_segment = PAGE_SIZE;
  2019. goto rebuild_st;
  2020. } else {
  2021. dev_warn(&dev_priv->drm.pdev->dev,
  2022. "Failed to DMA remap %lu pages\n",
  2023. page_count);
  2024. goto err_pages;
  2025. }
  2026. }
  2027. if (i915_gem_object_needs_bit17_swizzle(obj))
  2028. i915_gem_object_do_bit_17_swizzle(obj, st);
  2029. return st;
  2030. err_sg:
  2031. sg_mark_end(sg);
  2032. err_pages:
  2033. for_each_sgt_page(page, sgt_iter, st)
  2034. put_page(page);
  2035. sg_free_table(st);
  2036. kfree(st);
  2037. /* shmemfs first checks if there is enough memory to allocate the page
  2038. * and reports ENOSPC should there be insufficient, along with the usual
  2039. * ENOMEM for a genuine allocation failure.
  2040. *
  2041. * We use ENOSPC in our driver to mean that we have run out of aperture
  2042. * space and so want to translate the error from shmemfs back to our
  2043. * usual understanding of ENOMEM.
  2044. */
  2045. if (ret == -ENOSPC)
  2046. ret = -ENOMEM;
  2047. return ERR_PTR(ret);
  2048. }
  2049. void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
  2050. struct sg_table *pages)
  2051. {
  2052. lockdep_assert_held(&obj->mm.lock);
  2053. obj->mm.get_page.sg_pos = pages->sgl;
  2054. obj->mm.get_page.sg_idx = 0;
  2055. obj->mm.pages = pages;
  2056. if (i915_gem_object_is_tiled(obj) &&
  2057. to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  2058. GEM_BUG_ON(obj->mm.quirked);
  2059. __i915_gem_object_pin_pages(obj);
  2060. obj->mm.quirked = true;
  2061. }
  2062. }
  2063. static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2064. {
  2065. struct sg_table *pages;
  2066. GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
  2067. if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
  2068. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  2069. return -EFAULT;
  2070. }
  2071. pages = obj->ops->get_pages(obj);
  2072. if (unlikely(IS_ERR(pages)))
  2073. return PTR_ERR(pages);
  2074. __i915_gem_object_set_pages(obj, pages);
  2075. return 0;
  2076. }
  2077. /* Ensure that the associated pages are gathered from the backing storage
  2078. * and pinned into our object. i915_gem_object_pin_pages() may be called
  2079. * multiple times before they are released by a single call to
  2080. * i915_gem_object_unpin_pages() - once the pages are no longer referenced
  2081. * either as a result of memory pressure (reaping pages under the shrinker)
  2082. * or as the object is itself released.
  2083. */
  2084. int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2085. {
  2086. int err;
  2087. err = mutex_lock_interruptible(&obj->mm.lock);
  2088. if (err)
  2089. return err;
  2090. if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
  2091. err = ____i915_gem_object_get_pages(obj);
  2092. if (err)
  2093. goto unlock;
  2094. smp_mb__before_atomic();
  2095. }
  2096. atomic_inc(&obj->mm.pages_pin_count);
  2097. unlock:
  2098. mutex_unlock(&obj->mm.lock);
  2099. return err;
  2100. }
  2101. /* The 'mapping' part of i915_gem_object_pin_map() below */
  2102. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
  2103. enum i915_map_type type)
  2104. {
  2105. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  2106. struct sg_table *sgt = obj->mm.pages;
  2107. struct sgt_iter sgt_iter;
  2108. struct page *page;
  2109. struct page *stack_pages[32];
  2110. struct page **pages = stack_pages;
  2111. unsigned long i = 0;
  2112. pgprot_t pgprot;
  2113. void *addr;
  2114. /* A single page can always be kmapped */
  2115. if (n_pages == 1 && type == I915_MAP_WB)
  2116. return kmap(sg_page(sgt->sgl));
  2117. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2118. /* Too big for stack -- allocate temporary array instead */
  2119. pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
  2120. if (!pages)
  2121. return NULL;
  2122. }
  2123. for_each_sgt_page(page, sgt_iter, sgt)
  2124. pages[i++] = page;
  2125. /* Check that we have the expected number of pages */
  2126. GEM_BUG_ON(i != n_pages);
  2127. switch (type) {
  2128. case I915_MAP_WB:
  2129. pgprot = PAGE_KERNEL;
  2130. break;
  2131. case I915_MAP_WC:
  2132. pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
  2133. break;
  2134. }
  2135. addr = vmap(pages, n_pages, 0, pgprot);
  2136. if (pages != stack_pages)
  2137. drm_free_large(pages);
  2138. return addr;
  2139. }
  2140. /* get, pin, and map the pages of the object into kernel space */
  2141. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
  2142. enum i915_map_type type)
  2143. {
  2144. enum i915_map_type has_type;
  2145. bool pinned;
  2146. void *ptr;
  2147. int ret;
  2148. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  2149. ret = mutex_lock_interruptible(&obj->mm.lock);
  2150. if (ret)
  2151. return ERR_PTR(ret);
  2152. pinned = true;
  2153. if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
  2154. if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
  2155. ret = ____i915_gem_object_get_pages(obj);
  2156. if (ret)
  2157. goto err_unlock;
  2158. smp_mb__before_atomic();
  2159. }
  2160. atomic_inc(&obj->mm.pages_pin_count);
  2161. pinned = false;
  2162. }
  2163. GEM_BUG_ON(!obj->mm.pages);
  2164. ptr = ptr_unpack_bits(obj->mm.mapping, has_type);
  2165. if (ptr && has_type != type) {
  2166. if (pinned) {
  2167. ret = -EBUSY;
  2168. goto err_unpin;
  2169. }
  2170. if (is_vmalloc_addr(ptr))
  2171. vunmap(ptr);
  2172. else
  2173. kunmap(kmap_to_page(ptr));
  2174. ptr = obj->mm.mapping = NULL;
  2175. }
  2176. if (!ptr) {
  2177. ptr = i915_gem_object_map(obj, type);
  2178. if (!ptr) {
  2179. ret = -ENOMEM;
  2180. goto err_unpin;
  2181. }
  2182. obj->mm.mapping = ptr_pack_bits(ptr, type);
  2183. }
  2184. out_unlock:
  2185. mutex_unlock(&obj->mm.lock);
  2186. return ptr;
  2187. err_unpin:
  2188. atomic_dec(&obj->mm.pages_pin_count);
  2189. err_unlock:
  2190. ptr = ERR_PTR(ret);
  2191. goto out_unlock;
  2192. }
  2193. static int
  2194. i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
  2195. const struct drm_i915_gem_pwrite *arg)
  2196. {
  2197. struct address_space *mapping = obj->base.filp->f_mapping;
  2198. char __user *user_data = u64_to_user_ptr(arg->data_ptr);
  2199. u64 remain, offset;
  2200. unsigned int pg;
  2201. /* Before we instantiate/pin the backing store for our use, we
  2202. * can prepopulate the shmemfs filp efficiently using a write into
  2203. * the pagecache. We avoid the penalty of instantiating all the
  2204. * pages, important if the user is just writing to a few and never
  2205. * uses the object on the GPU, and using a direct write into shmemfs
  2206. * allows it to avoid the cost of retrieving a page (either swapin
  2207. * or clearing-before-use) before it is overwritten.
  2208. */
  2209. if (READ_ONCE(obj->mm.pages))
  2210. return -ENODEV;
  2211. /* Before the pages are instantiated the object is treated as being
  2212. * in the CPU domain. The pages will be clflushed as required before
  2213. * use, and we can freely write into the pages directly. If userspace
  2214. * races pwrite with any other operation; corruption will ensue -
  2215. * that is userspace's prerogative!
  2216. */
  2217. remain = arg->size;
  2218. offset = arg->offset;
  2219. pg = offset_in_page(offset);
  2220. do {
  2221. unsigned int len, unwritten;
  2222. struct page *page;
  2223. void *data, *vaddr;
  2224. int err;
  2225. len = PAGE_SIZE - pg;
  2226. if (len > remain)
  2227. len = remain;
  2228. err = pagecache_write_begin(obj->base.filp, mapping,
  2229. offset, len, 0,
  2230. &page, &data);
  2231. if (err < 0)
  2232. return err;
  2233. vaddr = kmap(page);
  2234. unwritten = copy_from_user(vaddr + pg, user_data, len);
  2235. kunmap(page);
  2236. err = pagecache_write_end(obj->base.filp, mapping,
  2237. offset, len, len - unwritten,
  2238. page, data);
  2239. if (err < 0)
  2240. return err;
  2241. if (unwritten)
  2242. return -EFAULT;
  2243. remain -= len;
  2244. user_data += len;
  2245. offset += len;
  2246. pg = 0;
  2247. } while (remain);
  2248. return 0;
  2249. }
  2250. static bool ban_context(const struct i915_gem_context *ctx)
  2251. {
  2252. return (i915_gem_context_is_bannable(ctx) &&
  2253. ctx->ban_score >= CONTEXT_SCORE_BAN_THRESHOLD);
  2254. }
  2255. static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
  2256. {
  2257. ctx->guilty_count++;
  2258. ctx->ban_score += CONTEXT_SCORE_GUILTY;
  2259. if (ban_context(ctx))
  2260. i915_gem_context_set_banned(ctx);
  2261. DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
  2262. ctx->name, ctx->ban_score,
  2263. yesno(i915_gem_context_is_banned(ctx)));
  2264. if (!i915_gem_context_is_banned(ctx) || IS_ERR_OR_NULL(ctx->file_priv))
  2265. return;
  2266. ctx->file_priv->context_bans++;
  2267. DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
  2268. ctx->name, ctx->file_priv->context_bans);
  2269. }
  2270. static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
  2271. {
  2272. ctx->active_count++;
  2273. }
  2274. struct drm_i915_gem_request *
  2275. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2276. {
  2277. struct drm_i915_gem_request *request, *active = NULL;
  2278. unsigned long flags;
  2279. /* We are called by the error capture and reset at a random
  2280. * point in time. In particular, note that neither is crucially
  2281. * ordered with an interrupt. After a hang, the GPU is dead and we
  2282. * assume that no more writes can happen (we waited long enough for
  2283. * all writes that were in transaction to be flushed) - adding an
  2284. * extra delay for a recent interrupt is pointless. Hence, we do
  2285. * not need an engine->irq_seqno_barrier() before the seqno reads.
  2286. */
  2287. spin_lock_irqsave(&engine->timeline->lock, flags);
  2288. list_for_each_entry(request, &engine->timeline->requests, link) {
  2289. if (__i915_gem_request_completed(request,
  2290. request->global_seqno))
  2291. continue;
  2292. GEM_BUG_ON(request->engine != engine);
  2293. GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  2294. &request->fence.flags));
  2295. active = request;
  2296. break;
  2297. }
  2298. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2299. return active;
  2300. }
  2301. static bool engine_stalled(struct intel_engine_cs *engine)
  2302. {
  2303. if (!engine->hangcheck.stalled)
  2304. return false;
  2305. /* Check for possible seqno movement after hang declaration */
  2306. if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
  2307. DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
  2308. return false;
  2309. }
  2310. return true;
  2311. }
  2312. int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
  2313. {
  2314. struct intel_engine_cs *engine;
  2315. enum intel_engine_id id;
  2316. int err = 0;
  2317. /* Ensure irq handler finishes, and not run again. */
  2318. for_each_engine(engine, dev_priv, id) {
  2319. struct drm_i915_gem_request *request;
  2320. /* Prevent the signaler thread from updating the request
  2321. * state (by calling dma_fence_signal) as we are processing
  2322. * the reset. The write from the GPU of the seqno is
  2323. * asynchronous and the signaler thread may see a different
  2324. * value to us and declare the request complete, even though
  2325. * the reset routine have picked that request as the active
  2326. * (incomplete) request. This conflict is not handled
  2327. * gracefully!
  2328. */
  2329. kthread_park(engine->breadcrumbs.signaler);
  2330. /* Prevent request submission to the hardware until we have
  2331. * completed the reset in i915_gem_reset_finish(). If a request
  2332. * is completed by one engine, it may then queue a request
  2333. * to a second via its engine->irq_tasklet *just* as we are
  2334. * calling engine->init_hw() and also writing the ELSP.
  2335. * Turning off the engine->irq_tasklet until the reset is over
  2336. * prevents the race.
  2337. */
  2338. tasklet_kill(&engine->irq_tasklet);
  2339. tasklet_disable(&engine->irq_tasklet);
  2340. if (engine->irq_seqno_barrier)
  2341. engine->irq_seqno_barrier(engine);
  2342. if (engine_stalled(engine)) {
  2343. request = i915_gem_find_active_request(engine);
  2344. if (request && request->fence.error == -EIO)
  2345. err = -EIO; /* Previous reset failed! */
  2346. }
  2347. }
  2348. i915_gem_revoke_fences(dev_priv);
  2349. return err;
  2350. }
  2351. static void skip_request(struct drm_i915_gem_request *request)
  2352. {
  2353. void *vaddr = request->ring->vaddr;
  2354. u32 head;
  2355. /* As this request likely depends on state from the lost
  2356. * context, clear out all the user operations leaving the
  2357. * breadcrumb at the end (so we get the fence notifications).
  2358. */
  2359. head = request->head;
  2360. if (request->postfix < head) {
  2361. memset(vaddr + head, 0, request->ring->size - head);
  2362. head = 0;
  2363. }
  2364. memset(vaddr + head, 0, request->postfix - head);
  2365. dma_fence_set_error(&request->fence, -EIO);
  2366. }
  2367. static void engine_skip_context(struct drm_i915_gem_request *request)
  2368. {
  2369. struct intel_engine_cs *engine = request->engine;
  2370. struct i915_gem_context *hung_ctx = request->ctx;
  2371. struct intel_timeline *timeline;
  2372. unsigned long flags;
  2373. timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
  2374. spin_lock_irqsave(&engine->timeline->lock, flags);
  2375. spin_lock(&timeline->lock);
  2376. list_for_each_entry_continue(request, &engine->timeline->requests, link)
  2377. if (request->ctx == hung_ctx)
  2378. skip_request(request);
  2379. list_for_each_entry(request, &timeline->requests, link)
  2380. skip_request(request);
  2381. spin_unlock(&timeline->lock);
  2382. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2383. }
  2384. /* Returns true if the request was guilty of hang */
  2385. static bool i915_gem_reset_request(struct drm_i915_gem_request *request)
  2386. {
  2387. /* Read once and return the resolution */
  2388. const bool guilty = engine_stalled(request->engine);
  2389. /* The guilty request will get skipped on a hung engine.
  2390. *
  2391. * Users of client default contexts do not rely on logical
  2392. * state preserved between batches so it is safe to execute
  2393. * queued requests following the hang. Non default contexts
  2394. * rely on preserved state, so skipping a batch loses the
  2395. * evolution of the state and it needs to be considered corrupted.
  2396. * Executing more queued batches on top of corrupted state is
  2397. * risky. But we take the risk by trying to advance through
  2398. * the queued requests in order to make the client behaviour
  2399. * more predictable around resets, by not throwing away random
  2400. * amount of batches it has prepared for execution. Sophisticated
  2401. * clients can use gem_reset_stats_ioctl and dma fence status
  2402. * (exported via sync_file info ioctl on explicit fences) to observe
  2403. * when it loses the context state and should rebuild accordingly.
  2404. *
  2405. * The context ban, and ultimately the client ban, mechanism are safety
  2406. * valves if client submission ends up resulting in nothing more than
  2407. * subsequent hangs.
  2408. */
  2409. if (guilty) {
  2410. i915_gem_context_mark_guilty(request->ctx);
  2411. skip_request(request);
  2412. } else {
  2413. i915_gem_context_mark_innocent(request->ctx);
  2414. dma_fence_set_error(&request->fence, -EAGAIN);
  2415. }
  2416. return guilty;
  2417. }
  2418. static void i915_gem_reset_engine(struct intel_engine_cs *engine)
  2419. {
  2420. struct drm_i915_gem_request *request;
  2421. request = i915_gem_find_active_request(engine);
  2422. if (request && i915_gem_reset_request(request)) {
  2423. DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
  2424. engine->name, request->global_seqno);
  2425. /* If this context is now banned, skip all pending requests. */
  2426. if (i915_gem_context_is_banned(request->ctx))
  2427. engine_skip_context(request);
  2428. }
  2429. /* Setup the CS to resume from the breadcrumb of the hung request */
  2430. engine->reset_hw(engine, request);
  2431. }
  2432. void i915_gem_reset(struct drm_i915_private *dev_priv)
  2433. {
  2434. struct intel_engine_cs *engine;
  2435. enum intel_engine_id id;
  2436. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2437. i915_gem_retire_requests(dev_priv);
  2438. for_each_engine(engine, dev_priv, id) {
  2439. struct i915_gem_context *ctx;
  2440. i915_gem_reset_engine(engine);
  2441. ctx = fetch_and_zero(&engine->last_retired_context);
  2442. if (ctx)
  2443. engine->context_unpin(engine, ctx);
  2444. }
  2445. i915_gem_restore_fences(dev_priv);
  2446. if (dev_priv->gt.awake) {
  2447. intel_sanitize_gt_powersave(dev_priv);
  2448. intel_enable_gt_powersave(dev_priv);
  2449. if (INTEL_GEN(dev_priv) >= 6)
  2450. gen6_rps_busy(dev_priv);
  2451. }
  2452. }
  2453. void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
  2454. {
  2455. struct intel_engine_cs *engine;
  2456. enum intel_engine_id id;
  2457. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2458. for_each_engine(engine, dev_priv, id) {
  2459. tasklet_enable(&engine->irq_tasklet);
  2460. kthread_unpark(engine->breadcrumbs.signaler);
  2461. }
  2462. }
  2463. static void nop_submit_request(struct drm_i915_gem_request *request)
  2464. {
  2465. dma_fence_set_error(&request->fence, -EIO);
  2466. i915_gem_request_submit(request);
  2467. intel_engine_init_global_seqno(request->engine, request->global_seqno);
  2468. }
  2469. static void engine_set_wedged(struct intel_engine_cs *engine)
  2470. {
  2471. struct drm_i915_gem_request *request;
  2472. unsigned long flags;
  2473. /* We need to be sure that no thread is running the old callback as
  2474. * we install the nop handler (otherwise we would submit a request
  2475. * to hardware that will never complete). In order to prevent this
  2476. * race, we wait until the machine is idle before making the swap
  2477. * (using stop_machine()).
  2478. */
  2479. engine->submit_request = nop_submit_request;
  2480. /* Mark all executing requests as skipped */
  2481. spin_lock_irqsave(&engine->timeline->lock, flags);
  2482. list_for_each_entry(request, &engine->timeline->requests, link)
  2483. dma_fence_set_error(&request->fence, -EIO);
  2484. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2485. /* Mark all pending requests as complete so that any concurrent
  2486. * (lockless) lookup doesn't try and wait upon the request as we
  2487. * reset it.
  2488. */
  2489. intel_engine_init_global_seqno(engine,
  2490. intel_engine_last_submit(engine));
  2491. /*
  2492. * Clear the execlists queue up before freeing the requests, as those
  2493. * are the ones that keep the context and ringbuffer backing objects
  2494. * pinned in place.
  2495. */
  2496. if (i915.enable_execlists) {
  2497. unsigned long flags;
  2498. spin_lock_irqsave(&engine->timeline->lock, flags);
  2499. i915_gem_request_put(engine->execlist_port[0].request);
  2500. i915_gem_request_put(engine->execlist_port[1].request);
  2501. memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
  2502. engine->execlist_queue = RB_ROOT;
  2503. engine->execlist_first = NULL;
  2504. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2505. }
  2506. }
  2507. static int __i915_gem_set_wedged_BKL(void *data)
  2508. {
  2509. struct drm_i915_private *i915 = data;
  2510. struct intel_engine_cs *engine;
  2511. enum intel_engine_id id;
  2512. for_each_engine(engine, i915, id)
  2513. engine_set_wedged(engine);
  2514. return 0;
  2515. }
  2516. void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
  2517. {
  2518. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2519. set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
  2520. /* Retire completed requests first so the list of inflight/incomplete
  2521. * requests is accurate and we don't try and mark successful requests
  2522. * as in error during __i915_gem_set_wedged_BKL().
  2523. */
  2524. i915_gem_retire_requests(dev_priv);
  2525. stop_machine(__i915_gem_set_wedged_BKL, dev_priv, NULL);
  2526. i915_gem_context_lost(dev_priv);
  2527. mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
  2528. }
  2529. bool i915_gem_unset_wedged(struct drm_i915_private *i915)
  2530. {
  2531. struct i915_gem_timeline *tl;
  2532. int i;
  2533. lockdep_assert_held(&i915->drm.struct_mutex);
  2534. if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
  2535. return true;
  2536. /* Before unwedging, make sure that all pending operations
  2537. * are flushed and errored out - we may have requests waiting upon
  2538. * third party fences. We marked all inflight requests as EIO, and
  2539. * every execbuf since returned EIO, for consistency we want all
  2540. * the currently pending requests to also be marked as EIO, which
  2541. * is done inside our nop_submit_request - and so we must wait.
  2542. *
  2543. * No more can be submitted until we reset the wedged bit.
  2544. */
  2545. list_for_each_entry(tl, &i915->gt.timelines, link) {
  2546. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2547. struct drm_i915_gem_request *rq;
  2548. rq = i915_gem_active_peek(&tl->engine[i].last_request,
  2549. &i915->drm.struct_mutex);
  2550. if (!rq)
  2551. continue;
  2552. /* We can't use our normal waiter as we want to
  2553. * avoid recursively trying to handle the current
  2554. * reset. The basic dma_fence_default_wait() installs
  2555. * a callback for dma_fence_signal(), which is
  2556. * triggered by our nop handler (indirectly, the
  2557. * callback enables the signaler thread which is
  2558. * woken by the nop_submit_request() advancing the seqno
  2559. * and when the seqno passes the fence, the signaler
  2560. * then signals the fence waking us up).
  2561. */
  2562. if (dma_fence_default_wait(&rq->fence, true,
  2563. MAX_SCHEDULE_TIMEOUT) < 0)
  2564. return false;
  2565. }
  2566. }
  2567. /* Undo nop_submit_request. We prevent all new i915 requests from
  2568. * being queued (by disallowing execbuf whilst wedged) so having
  2569. * waited for all active requests above, we know the system is idle
  2570. * and do not have to worry about a thread being inside
  2571. * engine->submit_request() as we swap over. So unlike installing
  2572. * the nop_submit_request on reset, we can do this from normal
  2573. * context and do not require stop_machine().
  2574. */
  2575. intel_engines_reset_default_submission(i915);
  2576. smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
  2577. clear_bit(I915_WEDGED, &i915->gpu_error.flags);
  2578. return true;
  2579. }
  2580. static void
  2581. i915_gem_retire_work_handler(struct work_struct *work)
  2582. {
  2583. struct drm_i915_private *dev_priv =
  2584. container_of(work, typeof(*dev_priv), gt.retire_work.work);
  2585. struct drm_device *dev = &dev_priv->drm;
  2586. /* Come back later if the device is busy... */
  2587. if (mutex_trylock(&dev->struct_mutex)) {
  2588. i915_gem_retire_requests(dev_priv);
  2589. mutex_unlock(&dev->struct_mutex);
  2590. }
  2591. /* Keep the retire handler running until we are finally idle.
  2592. * We do not need to do this test under locking as in the worst-case
  2593. * we queue the retire worker once too often.
  2594. */
  2595. if (READ_ONCE(dev_priv->gt.awake)) {
  2596. i915_queue_hangcheck(dev_priv);
  2597. queue_delayed_work(dev_priv->wq,
  2598. &dev_priv->gt.retire_work,
  2599. round_jiffies_up_relative(HZ));
  2600. }
  2601. }
  2602. static void
  2603. i915_gem_idle_work_handler(struct work_struct *work)
  2604. {
  2605. struct drm_i915_private *dev_priv =
  2606. container_of(work, typeof(*dev_priv), gt.idle_work.work);
  2607. struct drm_device *dev = &dev_priv->drm;
  2608. struct intel_engine_cs *engine;
  2609. enum intel_engine_id id;
  2610. bool rearm_hangcheck;
  2611. if (!READ_ONCE(dev_priv->gt.awake))
  2612. return;
  2613. /*
  2614. * Wait for last execlists context complete, but bail out in case a
  2615. * new request is submitted.
  2616. */
  2617. wait_for(intel_engines_are_idle(dev_priv), 10);
  2618. if (READ_ONCE(dev_priv->gt.active_requests))
  2619. return;
  2620. rearm_hangcheck =
  2621. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  2622. if (!mutex_trylock(&dev->struct_mutex)) {
  2623. /* Currently busy, come back later */
  2624. mod_delayed_work(dev_priv->wq,
  2625. &dev_priv->gt.idle_work,
  2626. msecs_to_jiffies(50));
  2627. goto out_rearm;
  2628. }
  2629. /*
  2630. * New request retired after this work handler started, extend active
  2631. * period until next instance of the work.
  2632. */
  2633. if (work_pending(work))
  2634. goto out_unlock;
  2635. if (dev_priv->gt.active_requests)
  2636. goto out_unlock;
  2637. if (wait_for(intel_engines_are_idle(dev_priv), 10))
  2638. DRM_ERROR("Timeout waiting for engines to idle\n");
  2639. for_each_engine(engine, dev_priv, id) {
  2640. intel_engine_disarm_breadcrumbs(engine);
  2641. i915_gem_batch_pool_fini(&engine->batch_pool);
  2642. }
  2643. GEM_BUG_ON(!dev_priv->gt.awake);
  2644. dev_priv->gt.awake = false;
  2645. rearm_hangcheck = false;
  2646. if (INTEL_GEN(dev_priv) >= 6)
  2647. gen6_rps_idle(dev_priv);
  2648. intel_runtime_pm_put(dev_priv);
  2649. out_unlock:
  2650. mutex_unlock(&dev->struct_mutex);
  2651. out_rearm:
  2652. if (rearm_hangcheck) {
  2653. GEM_BUG_ON(!dev_priv->gt.awake);
  2654. i915_queue_hangcheck(dev_priv);
  2655. }
  2656. }
  2657. void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
  2658. {
  2659. struct drm_i915_gem_object *obj = to_intel_bo(gem);
  2660. struct drm_i915_file_private *fpriv = file->driver_priv;
  2661. struct i915_vma *vma, *vn;
  2662. mutex_lock(&obj->base.dev->struct_mutex);
  2663. list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
  2664. if (vma->vm->file == fpriv)
  2665. i915_vma_close(vma);
  2666. if (i915_gem_object_is_active(obj) &&
  2667. !i915_gem_object_has_active_reference(obj)) {
  2668. i915_gem_object_set_active_reference(obj);
  2669. i915_gem_object_get(obj);
  2670. }
  2671. mutex_unlock(&obj->base.dev->struct_mutex);
  2672. }
  2673. static unsigned long to_wait_timeout(s64 timeout_ns)
  2674. {
  2675. if (timeout_ns < 0)
  2676. return MAX_SCHEDULE_TIMEOUT;
  2677. if (timeout_ns == 0)
  2678. return 0;
  2679. return nsecs_to_jiffies_timeout(timeout_ns);
  2680. }
  2681. /**
  2682. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2683. * @dev: drm device pointer
  2684. * @data: ioctl data blob
  2685. * @file: drm file pointer
  2686. *
  2687. * Returns 0 if successful, else an error is returned with the remaining time in
  2688. * the timeout parameter.
  2689. * -ETIME: object is still busy after timeout
  2690. * -ERESTARTSYS: signal interrupted the wait
  2691. * -ENONENT: object doesn't exist
  2692. * Also possible, but rare:
  2693. * -EAGAIN: GPU wedged
  2694. * -ENOMEM: damn
  2695. * -ENODEV: Internal IRQ fail
  2696. * -E?: The add request failed
  2697. *
  2698. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2699. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2700. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2701. * without holding struct_mutex the object may become re-busied before this
  2702. * function completes. A similar but shorter * race condition exists in the busy
  2703. * ioctl
  2704. */
  2705. int
  2706. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2707. {
  2708. struct drm_i915_gem_wait *args = data;
  2709. struct drm_i915_gem_object *obj;
  2710. ktime_t start;
  2711. long ret;
  2712. if (args->flags != 0)
  2713. return -EINVAL;
  2714. obj = i915_gem_object_lookup(file, args->bo_handle);
  2715. if (!obj)
  2716. return -ENOENT;
  2717. start = ktime_get();
  2718. ret = i915_gem_object_wait(obj,
  2719. I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
  2720. to_wait_timeout(args->timeout_ns),
  2721. to_rps_client(file));
  2722. if (args->timeout_ns > 0) {
  2723. args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
  2724. if (args->timeout_ns < 0)
  2725. args->timeout_ns = 0;
  2726. /*
  2727. * Apparently ktime isn't accurate enough and occasionally has a
  2728. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  2729. * things up to make the test happy. We allow up to 1 jiffy.
  2730. *
  2731. * This is a regression from the timespec->ktime conversion.
  2732. */
  2733. if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
  2734. args->timeout_ns = 0;
  2735. }
  2736. i915_gem_object_put(obj);
  2737. return ret;
  2738. }
  2739. static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
  2740. {
  2741. int ret, i;
  2742. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2743. ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
  2744. if (ret)
  2745. return ret;
  2746. }
  2747. return 0;
  2748. }
  2749. static int wait_for_engine(struct intel_engine_cs *engine, int timeout_ms)
  2750. {
  2751. return wait_for(intel_engine_is_idle(engine), timeout_ms);
  2752. }
  2753. static int wait_for_engines(struct drm_i915_private *i915)
  2754. {
  2755. struct intel_engine_cs *engine;
  2756. enum intel_engine_id id;
  2757. for_each_engine(engine, i915, id) {
  2758. if (GEM_WARN_ON(wait_for_engine(engine, 50))) {
  2759. i915_gem_set_wedged(i915);
  2760. return -EIO;
  2761. }
  2762. GEM_BUG_ON(intel_engine_get_seqno(engine) !=
  2763. intel_engine_last_submit(engine));
  2764. }
  2765. return 0;
  2766. }
  2767. int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
  2768. {
  2769. int ret;
  2770. if (flags & I915_WAIT_LOCKED) {
  2771. struct i915_gem_timeline *tl;
  2772. lockdep_assert_held(&i915->drm.struct_mutex);
  2773. list_for_each_entry(tl, &i915->gt.timelines, link) {
  2774. ret = wait_for_timeline(tl, flags);
  2775. if (ret)
  2776. return ret;
  2777. }
  2778. i915_gem_retire_requests(i915);
  2779. GEM_BUG_ON(i915->gt.active_requests);
  2780. ret = wait_for_engines(i915);
  2781. } else {
  2782. ret = wait_for_timeline(&i915->gt.global_timeline, flags);
  2783. }
  2784. return ret;
  2785. }
  2786. /** Flushes the GTT write domain for the object if it's dirty. */
  2787. static void
  2788. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2789. {
  2790. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2791. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2792. return;
  2793. /* No actual flushing is required for the GTT write domain. Writes
  2794. * to it "immediately" go to main memory as far as we know, so there's
  2795. * no chipset flush. It also doesn't land in render cache.
  2796. *
  2797. * However, we do have to enforce the order so that all writes through
  2798. * the GTT land before any writes to the device, such as updates to
  2799. * the GATT itself.
  2800. *
  2801. * We also have to wait a bit for the writes to land from the GTT.
  2802. * An uncached read (i.e. mmio) seems to be ideal for the round-trip
  2803. * timing. This issue has only been observed when switching quickly
  2804. * between GTT writes and CPU reads from inside the kernel on recent hw,
  2805. * and it appears to only affect discrete GTT blocks (i.e. on LLC
  2806. * system agents we cannot reproduce this behaviour).
  2807. */
  2808. wmb();
  2809. if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv)) {
  2810. if (intel_runtime_pm_get_if_in_use(dev_priv)) {
  2811. spin_lock_irq(&dev_priv->uncore.lock);
  2812. POSTING_READ_FW(RING_ACTHD(dev_priv->engine[RCS]->mmio_base));
  2813. spin_unlock_irq(&dev_priv->uncore.lock);
  2814. intel_runtime_pm_put(dev_priv);
  2815. }
  2816. }
  2817. intel_fb_obj_flush(obj, write_origin(obj, I915_GEM_DOMAIN_GTT));
  2818. obj->base.write_domain = 0;
  2819. }
  2820. /** Flushes the CPU write domain for the object if it's dirty. */
  2821. static void
  2822. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2823. {
  2824. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2825. return;
  2826. i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
  2827. obj->base.write_domain = 0;
  2828. }
  2829. static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
  2830. {
  2831. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU && !obj->cache_dirty)
  2832. return;
  2833. i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
  2834. obj->base.write_domain = 0;
  2835. }
  2836. void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
  2837. {
  2838. if (!READ_ONCE(obj->pin_display))
  2839. return;
  2840. mutex_lock(&obj->base.dev->struct_mutex);
  2841. __i915_gem_object_flush_for_display(obj);
  2842. mutex_unlock(&obj->base.dev->struct_mutex);
  2843. }
  2844. /**
  2845. * Moves a single object to the GTT read, and possibly write domain.
  2846. * @obj: object to act on
  2847. * @write: ask for write access or read only
  2848. *
  2849. * This function returns when the move is complete, including waiting on
  2850. * flushes to occur.
  2851. */
  2852. int
  2853. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2854. {
  2855. int ret;
  2856. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2857. ret = i915_gem_object_wait(obj,
  2858. I915_WAIT_INTERRUPTIBLE |
  2859. I915_WAIT_LOCKED |
  2860. (write ? I915_WAIT_ALL : 0),
  2861. MAX_SCHEDULE_TIMEOUT,
  2862. NULL);
  2863. if (ret)
  2864. return ret;
  2865. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2866. return 0;
  2867. /* Flush and acquire obj->pages so that we are coherent through
  2868. * direct access in memory with previous cached writes through
  2869. * shmemfs and that our cache domain tracking remains valid.
  2870. * For example, if the obj->filp was moved to swap without us
  2871. * being notified and releasing the pages, we would mistakenly
  2872. * continue to assume that the obj remained out of the CPU cached
  2873. * domain.
  2874. */
  2875. ret = i915_gem_object_pin_pages(obj);
  2876. if (ret)
  2877. return ret;
  2878. i915_gem_object_flush_cpu_write_domain(obj);
  2879. /* Serialise direct access to this object with the barriers for
  2880. * coherent writes from the GPU, by effectively invalidating the
  2881. * GTT domain upon first access.
  2882. */
  2883. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2884. mb();
  2885. /* It should now be out of any other write domains, and we can update
  2886. * the domain values for our changes.
  2887. */
  2888. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2889. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2890. if (write) {
  2891. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2892. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2893. obj->mm.dirty = true;
  2894. }
  2895. i915_gem_object_unpin_pages(obj);
  2896. return 0;
  2897. }
  2898. /**
  2899. * Changes the cache-level of an object across all VMA.
  2900. * @obj: object to act on
  2901. * @cache_level: new cache level to set for the object
  2902. *
  2903. * After this function returns, the object will be in the new cache-level
  2904. * across all GTT and the contents of the backing storage will be coherent,
  2905. * with respect to the new cache-level. In order to keep the backing storage
  2906. * coherent for all users, we only allow a single cache level to be set
  2907. * globally on the object and prevent it from being changed whilst the
  2908. * hardware is reading from the object. That is if the object is currently
  2909. * on the scanout it will be set to uncached (or equivalent display
  2910. * cache coherency) and all non-MOCS GPU access will also be uncached so
  2911. * that all direct access to the scanout remains coherent.
  2912. */
  2913. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2914. enum i915_cache_level cache_level)
  2915. {
  2916. struct i915_vma *vma;
  2917. int ret;
  2918. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2919. if (obj->cache_level == cache_level)
  2920. return 0;
  2921. /* Inspect the list of currently bound VMA and unbind any that would
  2922. * be invalid given the new cache-level. This is principally to
  2923. * catch the issue of the CS prefetch crossing page boundaries and
  2924. * reading an invalid PTE on older architectures.
  2925. */
  2926. restart:
  2927. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2928. if (!drm_mm_node_allocated(&vma->node))
  2929. continue;
  2930. if (i915_vma_is_pinned(vma)) {
  2931. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2932. return -EBUSY;
  2933. }
  2934. if (i915_gem_valid_gtt_space(vma, cache_level))
  2935. continue;
  2936. ret = i915_vma_unbind(vma);
  2937. if (ret)
  2938. return ret;
  2939. /* As unbinding may affect other elements in the
  2940. * obj->vma_list (due to side-effects from retiring
  2941. * an active vma), play safe and restart the iterator.
  2942. */
  2943. goto restart;
  2944. }
  2945. /* We can reuse the existing drm_mm nodes but need to change the
  2946. * cache-level on the PTE. We could simply unbind them all and
  2947. * rebind with the correct cache-level on next use. However since
  2948. * we already have a valid slot, dma mapping, pages etc, we may as
  2949. * rewrite the PTE in the belief that doing so tramples upon less
  2950. * state and so involves less work.
  2951. */
  2952. if (obj->bind_count) {
  2953. /* Before we change the PTE, the GPU must not be accessing it.
  2954. * If we wait upon the object, we know that all the bound
  2955. * VMA are no longer active.
  2956. */
  2957. ret = i915_gem_object_wait(obj,
  2958. I915_WAIT_INTERRUPTIBLE |
  2959. I915_WAIT_LOCKED |
  2960. I915_WAIT_ALL,
  2961. MAX_SCHEDULE_TIMEOUT,
  2962. NULL);
  2963. if (ret)
  2964. return ret;
  2965. if (!HAS_LLC(to_i915(obj->base.dev)) &&
  2966. cache_level != I915_CACHE_NONE) {
  2967. /* Access to snoopable pages through the GTT is
  2968. * incoherent and on some machines causes a hard
  2969. * lockup. Relinquish the CPU mmaping to force
  2970. * userspace to refault in the pages and we can
  2971. * then double check if the GTT mapping is still
  2972. * valid for that pointer access.
  2973. */
  2974. i915_gem_release_mmap(obj);
  2975. /* As we no longer need a fence for GTT access,
  2976. * we can relinquish it now (and so prevent having
  2977. * to steal a fence from someone else on the next
  2978. * fence request). Note GPU activity would have
  2979. * dropped the fence as all snoopable access is
  2980. * supposed to be linear.
  2981. */
  2982. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2983. ret = i915_vma_put_fence(vma);
  2984. if (ret)
  2985. return ret;
  2986. }
  2987. } else {
  2988. /* We either have incoherent backing store and
  2989. * so no GTT access or the architecture is fully
  2990. * coherent. In such cases, existing GTT mmaps
  2991. * ignore the cache bit in the PTE and we can
  2992. * rewrite it without confusing the GPU or having
  2993. * to force userspace to fault back in its mmaps.
  2994. */
  2995. }
  2996. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2997. if (!drm_mm_node_allocated(&vma->node))
  2998. continue;
  2999. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3000. if (ret)
  3001. return ret;
  3002. }
  3003. }
  3004. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU &&
  3005. i915_gem_object_is_coherent(obj))
  3006. obj->cache_dirty = true;
  3007. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3008. vma->node.color = cache_level;
  3009. obj->cache_level = cache_level;
  3010. return 0;
  3011. }
  3012. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3013. struct drm_file *file)
  3014. {
  3015. struct drm_i915_gem_caching *args = data;
  3016. struct drm_i915_gem_object *obj;
  3017. int err = 0;
  3018. rcu_read_lock();
  3019. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3020. if (!obj) {
  3021. err = -ENOENT;
  3022. goto out;
  3023. }
  3024. switch (obj->cache_level) {
  3025. case I915_CACHE_LLC:
  3026. case I915_CACHE_L3_LLC:
  3027. args->caching = I915_CACHING_CACHED;
  3028. break;
  3029. case I915_CACHE_WT:
  3030. args->caching = I915_CACHING_DISPLAY;
  3031. break;
  3032. default:
  3033. args->caching = I915_CACHING_NONE;
  3034. break;
  3035. }
  3036. out:
  3037. rcu_read_unlock();
  3038. return err;
  3039. }
  3040. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3041. struct drm_file *file)
  3042. {
  3043. struct drm_i915_private *i915 = to_i915(dev);
  3044. struct drm_i915_gem_caching *args = data;
  3045. struct drm_i915_gem_object *obj;
  3046. enum i915_cache_level level;
  3047. int ret = 0;
  3048. switch (args->caching) {
  3049. case I915_CACHING_NONE:
  3050. level = I915_CACHE_NONE;
  3051. break;
  3052. case I915_CACHING_CACHED:
  3053. /*
  3054. * Due to a HW issue on BXT A stepping, GPU stores via a
  3055. * snooped mapping may leave stale data in a corresponding CPU
  3056. * cacheline, whereas normally such cachelines would get
  3057. * invalidated.
  3058. */
  3059. if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
  3060. return -ENODEV;
  3061. level = I915_CACHE_LLC;
  3062. break;
  3063. case I915_CACHING_DISPLAY:
  3064. level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
  3065. break;
  3066. default:
  3067. return -EINVAL;
  3068. }
  3069. obj = i915_gem_object_lookup(file, args->handle);
  3070. if (!obj)
  3071. return -ENOENT;
  3072. if (obj->cache_level == level)
  3073. goto out;
  3074. ret = i915_gem_object_wait(obj,
  3075. I915_WAIT_INTERRUPTIBLE,
  3076. MAX_SCHEDULE_TIMEOUT,
  3077. to_rps_client(file));
  3078. if (ret)
  3079. goto out;
  3080. ret = i915_mutex_lock_interruptible(dev);
  3081. if (ret)
  3082. goto out;
  3083. ret = i915_gem_object_set_cache_level(obj, level);
  3084. mutex_unlock(&dev->struct_mutex);
  3085. out:
  3086. i915_gem_object_put(obj);
  3087. return ret;
  3088. }
  3089. /*
  3090. * Prepare buffer for display plane (scanout, cursors, etc).
  3091. * Can be called from an uninterruptible phase (modesetting) and allows
  3092. * any flushes to be pipelined (for pageflips).
  3093. */
  3094. struct i915_vma *
  3095. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3096. u32 alignment,
  3097. const struct i915_ggtt_view *view)
  3098. {
  3099. struct i915_vma *vma;
  3100. int ret;
  3101. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3102. /* Mark the pin_display early so that we account for the
  3103. * display coherency whilst setting up the cache domains.
  3104. */
  3105. obj->pin_display++;
  3106. /* The display engine is not coherent with the LLC cache on gen6. As
  3107. * a result, we make sure that the pinning that is about to occur is
  3108. * done with uncached PTEs. This is lowest common denominator for all
  3109. * chipsets.
  3110. *
  3111. * However for gen6+, we could do better by using the GFDT bit instead
  3112. * of uncaching, which would allow us to flush all the LLC-cached data
  3113. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3114. */
  3115. ret = i915_gem_object_set_cache_level(obj,
  3116. HAS_WT(to_i915(obj->base.dev)) ?
  3117. I915_CACHE_WT : I915_CACHE_NONE);
  3118. if (ret) {
  3119. vma = ERR_PTR(ret);
  3120. goto err_unpin_display;
  3121. }
  3122. /* As the user may map the buffer once pinned in the display plane
  3123. * (e.g. libkms for the bootup splash), we have to ensure that we
  3124. * always use map_and_fenceable for all scanout buffers. However,
  3125. * it may simply be too big to fit into mappable, in which case
  3126. * put it anyway and hope that userspace can cope (but always first
  3127. * try to preserve the existing ABI).
  3128. */
  3129. vma = ERR_PTR(-ENOSPC);
  3130. if (!view || view->type == I915_GGTT_VIEW_NORMAL)
  3131. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
  3132. PIN_MAPPABLE | PIN_NONBLOCK);
  3133. if (IS_ERR(vma)) {
  3134. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3135. unsigned int flags;
  3136. /* Valleyview is definitely limited to scanning out the first
  3137. * 512MiB. Lets presume this behaviour was inherited from the
  3138. * g4x display engine and that all earlier gen are similarly
  3139. * limited. Testing suggests that it is a little more
  3140. * complicated than this. For example, Cherryview appears quite
  3141. * happy to scanout from anywhere within its global aperture.
  3142. */
  3143. flags = 0;
  3144. if (HAS_GMCH_DISPLAY(i915))
  3145. flags = PIN_MAPPABLE;
  3146. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
  3147. }
  3148. if (IS_ERR(vma))
  3149. goto err_unpin_display;
  3150. vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
  3151. /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
  3152. __i915_gem_object_flush_for_display(obj);
  3153. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  3154. /* It should now be out of any other write domains, and we can update
  3155. * the domain values for our changes.
  3156. */
  3157. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3158. return vma;
  3159. err_unpin_display:
  3160. obj->pin_display--;
  3161. return vma;
  3162. }
  3163. void
  3164. i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
  3165. {
  3166. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  3167. if (WARN_ON(vma->obj->pin_display == 0))
  3168. return;
  3169. if (--vma->obj->pin_display == 0)
  3170. vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
  3171. /* Bump the LRU to try and avoid premature eviction whilst flipping */
  3172. i915_gem_object_bump_inactive_ggtt(vma->obj);
  3173. i915_vma_unpin(vma);
  3174. }
  3175. /**
  3176. * Moves a single object to the CPU read, and possibly write domain.
  3177. * @obj: object to act on
  3178. * @write: requesting write or read-only access
  3179. *
  3180. * This function returns when the move is complete, including waiting on
  3181. * flushes to occur.
  3182. */
  3183. int
  3184. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3185. {
  3186. int ret;
  3187. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3188. ret = i915_gem_object_wait(obj,
  3189. I915_WAIT_INTERRUPTIBLE |
  3190. I915_WAIT_LOCKED |
  3191. (write ? I915_WAIT_ALL : 0),
  3192. MAX_SCHEDULE_TIMEOUT,
  3193. NULL);
  3194. if (ret)
  3195. return ret;
  3196. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3197. return 0;
  3198. i915_gem_object_flush_gtt_write_domain(obj);
  3199. /* Flush the CPU cache if it's still invalid. */
  3200. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3201. i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
  3202. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3203. }
  3204. /* It should now be out of any other write domains, and we can update
  3205. * the domain values for our changes.
  3206. */
  3207. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3208. /* If we're writing through the CPU, then the GPU read domains will
  3209. * need to be invalidated at next use.
  3210. */
  3211. if (write) {
  3212. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3213. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3214. }
  3215. return 0;
  3216. }
  3217. /* Throttle our rendering by waiting until the ring has completed our requests
  3218. * emitted over 20 msec ago.
  3219. *
  3220. * Note that if we were to use the current jiffies each time around the loop,
  3221. * we wouldn't escape the function with any frames outstanding if the time to
  3222. * render a frame was over 20ms.
  3223. *
  3224. * This should get us reasonable parallelism between CPU and GPU but also
  3225. * relatively low latency when blocking on a particular request to finish.
  3226. */
  3227. static int
  3228. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3229. {
  3230. struct drm_i915_private *dev_priv = to_i915(dev);
  3231. struct drm_i915_file_private *file_priv = file->driver_priv;
  3232. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3233. struct drm_i915_gem_request *request, *target = NULL;
  3234. long ret;
  3235. /* ABI: return -EIO if already wedged */
  3236. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3237. return -EIO;
  3238. spin_lock(&file_priv->mm.lock);
  3239. list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
  3240. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3241. break;
  3242. if (target) {
  3243. list_del(&target->client_link);
  3244. target->file_priv = NULL;
  3245. }
  3246. target = request;
  3247. }
  3248. if (target)
  3249. i915_gem_request_get(target);
  3250. spin_unlock(&file_priv->mm.lock);
  3251. if (target == NULL)
  3252. return 0;
  3253. ret = i915_wait_request(target,
  3254. I915_WAIT_INTERRUPTIBLE,
  3255. MAX_SCHEDULE_TIMEOUT);
  3256. i915_gem_request_put(target);
  3257. return ret < 0 ? ret : 0;
  3258. }
  3259. struct i915_vma *
  3260. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3261. const struct i915_ggtt_view *view,
  3262. u64 size,
  3263. u64 alignment,
  3264. u64 flags)
  3265. {
  3266. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  3267. struct i915_address_space *vm = &dev_priv->ggtt.base;
  3268. struct i915_vma *vma;
  3269. int ret;
  3270. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3271. vma = i915_vma_instance(obj, vm, view);
  3272. if (unlikely(IS_ERR(vma)))
  3273. return vma;
  3274. if (i915_vma_misplaced(vma, size, alignment, flags)) {
  3275. if (flags & PIN_NONBLOCK &&
  3276. (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
  3277. return ERR_PTR(-ENOSPC);
  3278. if (flags & PIN_MAPPABLE) {
  3279. /* If the required space is larger than the available
  3280. * aperture, we will not able to find a slot for the
  3281. * object and unbinding the object now will be in
  3282. * vain. Worse, doing so may cause us to ping-pong
  3283. * the object in and out of the Global GTT and
  3284. * waste a lot of cycles under the mutex.
  3285. */
  3286. if (vma->fence_size > dev_priv->ggtt.mappable_end)
  3287. return ERR_PTR(-E2BIG);
  3288. /* If NONBLOCK is set the caller is optimistically
  3289. * trying to cache the full object within the mappable
  3290. * aperture, and *must* have a fallback in place for
  3291. * situations where we cannot bind the object. We
  3292. * can be a little more lax here and use the fallback
  3293. * more often to avoid costly migrations of ourselves
  3294. * and other objects within the aperture.
  3295. *
  3296. * Half-the-aperture is used as a simple heuristic.
  3297. * More interesting would to do search for a free
  3298. * block prior to making the commitment to unbind.
  3299. * That caters for the self-harm case, and with a
  3300. * little more heuristics (e.g. NOFAULT, NOEVICT)
  3301. * we could try to minimise harm to others.
  3302. */
  3303. if (flags & PIN_NONBLOCK &&
  3304. vma->fence_size > dev_priv->ggtt.mappable_end / 2)
  3305. return ERR_PTR(-ENOSPC);
  3306. }
  3307. WARN(i915_vma_is_pinned(vma),
  3308. "bo is already pinned in ggtt with incorrect alignment:"
  3309. " offset=%08x, req.alignment=%llx,"
  3310. " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
  3311. i915_ggtt_offset(vma), alignment,
  3312. !!(flags & PIN_MAPPABLE),
  3313. i915_vma_is_map_and_fenceable(vma));
  3314. ret = i915_vma_unbind(vma);
  3315. if (ret)
  3316. return ERR_PTR(ret);
  3317. }
  3318. ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
  3319. if (ret)
  3320. return ERR_PTR(ret);
  3321. return vma;
  3322. }
  3323. static __always_inline unsigned int __busy_read_flag(unsigned int id)
  3324. {
  3325. /* Note that we could alias engines in the execbuf API, but
  3326. * that would be very unwise as it prevents userspace from
  3327. * fine control over engine selection. Ahem.
  3328. *
  3329. * This should be something like EXEC_MAX_ENGINE instead of
  3330. * I915_NUM_ENGINES.
  3331. */
  3332. BUILD_BUG_ON(I915_NUM_ENGINES > 16);
  3333. return 0x10000 << id;
  3334. }
  3335. static __always_inline unsigned int __busy_write_id(unsigned int id)
  3336. {
  3337. /* The uABI guarantees an active writer is also amongst the read
  3338. * engines. This would be true if we accessed the activity tracking
  3339. * under the lock, but as we perform the lookup of the object and
  3340. * its activity locklessly we can not guarantee that the last_write
  3341. * being active implies that we have set the same engine flag from
  3342. * last_read - hence we always set both read and write busy for
  3343. * last_write.
  3344. */
  3345. return id | __busy_read_flag(id);
  3346. }
  3347. static __always_inline unsigned int
  3348. __busy_set_if_active(const struct dma_fence *fence,
  3349. unsigned int (*flag)(unsigned int id))
  3350. {
  3351. struct drm_i915_gem_request *rq;
  3352. /* We have to check the current hw status of the fence as the uABI
  3353. * guarantees forward progress. We could rely on the idle worker
  3354. * to eventually flush us, but to minimise latency just ask the
  3355. * hardware.
  3356. *
  3357. * Note we only report on the status of native fences.
  3358. */
  3359. if (!dma_fence_is_i915(fence))
  3360. return 0;
  3361. /* opencode to_request() in order to avoid const warnings */
  3362. rq = container_of(fence, struct drm_i915_gem_request, fence);
  3363. if (i915_gem_request_completed(rq))
  3364. return 0;
  3365. return flag(rq->engine->exec_id);
  3366. }
  3367. static __always_inline unsigned int
  3368. busy_check_reader(const struct dma_fence *fence)
  3369. {
  3370. return __busy_set_if_active(fence, __busy_read_flag);
  3371. }
  3372. static __always_inline unsigned int
  3373. busy_check_writer(const struct dma_fence *fence)
  3374. {
  3375. if (!fence)
  3376. return 0;
  3377. return __busy_set_if_active(fence, __busy_write_id);
  3378. }
  3379. int
  3380. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3381. struct drm_file *file)
  3382. {
  3383. struct drm_i915_gem_busy *args = data;
  3384. struct drm_i915_gem_object *obj;
  3385. struct reservation_object_list *list;
  3386. unsigned int seq;
  3387. int err;
  3388. err = -ENOENT;
  3389. rcu_read_lock();
  3390. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3391. if (!obj)
  3392. goto out;
  3393. /* A discrepancy here is that we do not report the status of
  3394. * non-i915 fences, i.e. even though we may report the object as idle,
  3395. * a call to set-domain may still stall waiting for foreign rendering.
  3396. * This also means that wait-ioctl may report an object as busy,
  3397. * where busy-ioctl considers it idle.
  3398. *
  3399. * We trade the ability to warn of foreign fences to report on which
  3400. * i915 engines are active for the object.
  3401. *
  3402. * Alternatively, we can trade that extra information on read/write
  3403. * activity with
  3404. * args->busy =
  3405. * !reservation_object_test_signaled_rcu(obj->resv, true);
  3406. * to report the overall busyness. This is what the wait-ioctl does.
  3407. *
  3408. */
  3409. retry:
  3410. seq = raw_read_seqcount(&obj->resv->seq);
  3411. /* Translate the exclusive fence to the READ *and* WRITE engine */
  3412. args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
  3413. /* Translate shared fences to READ set of engines */
  3414. list = rcu_dereference(obj->resv->fence);
  3415. if (list) {
  3416. unsigned int shared_count = list->shared_count, i;
  3417. for (i = 0; i < shared_count; ++i) {
  3418. struct dma_fence *fence =
  3419. rcu_dereference(list->shared[i]);
  3420. args->busy |= busy_check_reader(fence);
  3421. }
  3422. }
  3423. if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
  3424. goto retry;
  3425. err = 0;
  3426. out:
  3427. rcu_read_unlock();
  3428. return err;
  3429. }
  3430. int
  3431. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3432. struct drm_file *file_priv)
  3433. {
  3434. return i915_gem_ring_throttle(dev, file_priv);
  3435. }
  3436. int
  3437. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3438. struct drm_file *file_priv)
  3439. {
  3440. struct drm_i915_private *dev_priv = to_i915(dev);
  3441. struct drm_i915_gem_madvise *args = data;
  3442. struct drm_i915_gem_object *obj;
  3443. int err;
  3444. switch (args->madv) {
  3445. case I915_MADV_DONTNEED:
  3446. case I915_MADV_WILLNEED:
  3447. break;
  3448. default:
  3449. return -EINVAL;
  3450. }
  3451. obj = i915_gem_object_lookup(file_priv, args->handle);
  3452. if (!obj)
  3453. return -ENOENT;
  3454. err = mutex_lock_interruptible(&obj->mm.lock);
  3455. if (err)
  3456. goto out;
  3457. if (obj->mm.pages &&
  3458. i915_gem_object_is_tiled(obj) &&
  3459. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3460. if (obj->mm.madv == I915_MADV_WILLNEED) {
  3461. GEM_BUG_ON(!obj->mm.quirked);
  3462. __i915_gem_object_unpin_pages(obj);
  3463. obj->mm.quirked = false;
  3464. }
  3465. if (args->madv == I915_MADV_WILLNEED) {
  3466. GEM_BUG_ON(obj->mm.quirked);
  3467. __i915_gem_object_pin_pages(obj);
  3468. obj->mm.quirked = true;
  3469. }
  3470. }
  3471. if (obj->mm.madv != __I915_MADV_PURGED)
  3472. obj->mm.madv = args->madv;
  3473. /* if the object is no longer attached, discard its backing storage */
  3474. if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
  3475. i915_gem_object_truncate(obj);
  3476. args->retained = obj->mm.madv != __I915_MADV_PURGED;
  3477. mutex_unlock(&obj->mm.lock);
  3478. out:
  3479. i915_gem_object_put(obj);
  3480. return err;
  3481. }
  3482. static void
  3483. frontbuffer_retire(struct i915_gem_active *active,
  3484. struct drm_i915_gem_request *request)
  3485. {
  3486. struct drm_i915_gem_object *obj =
  3487. container_of(active, typeof(*obj), frontbuffer_write);
  3488. intel_fb_obj_flush(obj, ORIGIN_CS);
  3489. }
  3490. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3491. const struct drm_i915_gem_object_ops *ops)
  3492. {
  3493. mutex_init(&obj->mm.lock);
  3494. INIT_LIST_HEAD(&obj->global_link);
  3495. INIT_LIST_HEAD(&obj->userfault_link);
  3496. INIT_LIST_HEAD(&obj->obj_exec_link);
  3497. INIT_LIST_HEAD(&obj->vma_list);
  3498. INIT_LIST_HEAD(&obj->batch_pool_link);
  3499. obj->ops = ops;
  3500. reservation_object_init(&obj->__builtin_resv);
  3501. obj->resv = &obj->__builtin_resv;
  3502. obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
  3503. init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
  3504. obj->mm.madv = I915_MADV_WILLNEED;
  3505. INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
  3506. mutex_init(&obj->mm.get_page.lock);
  3507. i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
  3508. }
  3509. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3510. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
  3511. I915_GEM_OBJECT_IS_SHRINKABLE,
  3512. .get_pages = i915_gem_object_get_pages_gtt,
  3513. .put_pages = i915_gem_object_put_pages_gtt,
  3514. .pwrite = i915_gem_object_pwrite_gtt,
  3515. };
  3516. struct drm_i915_gem_object *
  3517. i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
  3518. {
  3519. struct drm_i915_gem_object *obj;
  3520. struct address_space *mapping;
  3521. gfp_t mask;
  3522. int ret;
  3523. /* There is a prevalence of the assumption that we fit the object's
  3524. * page count inside a 32bit _signed_ variable. Let's document this and
  3525. * catch if we ever need to fix it. In the meantime, if you do spot
  3526. * such a local variable, please consider fixing!
  3527. */
  3528. if (WARN_ON(size >> PAGE_SHIFT > INT_MAX))
  3529. return ERR_PTR(-E2BIG);
  3530. if (overflows_type(size, obj->base.size))
  3531. return ERR_PTR(-E2BIG);
  3532. obj = i915_gem_object_alloc(dev_priv);
  3533. if (obj == NULL)
  3534. return ERR_PTR(-ENOMEM);
  3535. ret = drm_gem_object_init(&dev_priv->drm, &obj->base, size);
  3536. if (ret)
  3537. goto fail;
  3538. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3539. if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
  3540. /* 965gm cannot relocate objects above 4GiB. */
  3541. mask &= ~__GFP_HIGHMEM;
  3542. mask |= __GFP_DMA32;
  3543. }
  3544. mapping = obj->base.filp->f_mapping;
  3545. mapping_set_gfp_mask(mapping, mask);
  3546. i915_gem_object_init(obj, &i915_gem_object_ops);
  3547. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3548. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3549. if (HAS_LLC(dev_priv)) {
  3550. /* On some devices, we can have the GPU use the LLC (the CPU
  3551. * cache) for about a 10% performance improvement
  3552. * compared to uncached. Graphics requests other than
  3553. * display scanout are coherent with the CPU in
  3554. * accessing this cache. This means in this mode we
  3555. * don't need to clflush on the CPU side, and on the
  3556. * GPU side we only need to flush internal caches to
  3557. * get data visible to the CPU.
  3558. *
  3559. * However, we maintain the display planes as UC, and so
  3560. * need to rebind when first used as such.
  3561. */
  3562. obj->cache_level = I915_CACHE_LLC;
  3563. } else
  3564. obj->cache_level = I915_CACHE_NONE;
  3565. trace_i915_gem_object_create(obj);
  3566. return obj;
  3567. fail:
  3568. i915_gem_object_free(obj);
  3569. return ERR_PTR(ret);
  3570. }
  3571. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3572. {
  3573. /* If we are the last user of the backing storage (be it shmemfs
  3574. * pages or stolen etc), we know that the pages are going to be
  3575. * immediately released. In this case, we can then skip copying
  3576. * back the contents from the GPU.
  3577. */
  3578. if (obj->mm.madv != I915_MADV_WILLNEED)
  3579. return false;
  3580. if (obj->base.filp == NULL)
  3581. return true;
  3582. /* At first glance, this looks racy, but then again so would be
  3583. * userspace racing mmap against close. However, the first external
  3584. * reference to the filp can only be obtained through the
  3585. * i915_gem_mmap_ioctl() which safeguards us against the user
  3586. * acquiring such a reference whilst we are in the middle of
  3587. * freeing the object.
  3588. */
  3589. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3590. }
  3591. static void __i915_gem_free_objects(struct drm_i915_private *i915,
  3592. struct llist_node *freed)
  3593. {
  3594. struct drm_i915_gem_object *obj, *on;
  3595. mutex_lock(&i915->drm.struct_mutex);
  3596. intel_runtime_pm_get(i915);
  3597. llist_for_each_entry(obj, freed, freed) {
  3598. struct i915_vma *vma, *vn;
  3599. trace_i915_gem_object_destroy(obj);
  3600. GEM_BUG_ON(i915_gem_object_is_active(obj));
  3601. list_for_each_entry_safe(vma, vn,
  3602. &obj->vma_list, obj_link) {
  3603. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  3604. GEM_BUG_ON(i915_vma_is_active(vma));
  3605. vma->flags &= ~I915_VMA_PIN_MASK;
  3606. i915_vma_close(vma);
  3607. }
  3608. GEM_BUG_ON(!list_empty(&obj->vma_list));
  3609. GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
  3610. list_del(&obj->global_link);
  3611. }
  3612. intel_runtime_pm_put(i915);
  3613. mutex_unlock(&i915->drm.struct_mutex);
  3614. llist_for_each_entry_safe(obj, on, freed, freed) {
  3615. GEM_BUG_ON(obj->bind_count);
  3616. GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
  3617. if (obj->ops->release)
  3618. obj->ops->release(obj);
  3619. if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
  3620. atomic_set(&obj->mm.pages_pin_count, 0);
  3621. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  3622. GEM_BUG_ON(obj->mm.pages);
  3623. if (obj->base.import_attach)
  3624. drm_prime_gem_destroy(&obj->base, NULL);
  3625. reservation_object_fini(&obj->__builtin_resv);
  3626. drm_gem_object_release(&obj->base);
  3627. i915_gem_info_remove_obj(i915, obj->base.size);
  3628. kfree(obj->bit_17);
  3629. i915_gem_object_free(obj);
  3630. }
  3631. }
  3632. static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
  3633. {
  3634. struct llist_node *freed;
  3635. freed = llist_del_all(&i915->mm.free_list);
  3636. if (unlikely(freed))
  3637. __i915_gem_free_objects(i915, freed);
  3638. }
  3639. static void __i915_gem_free_work(struct work_struct *work)
  3640. {
  3641. struct drm_i915_private *i915 =
  3642. container_of(work, struct drm_i915_private, mm.free_work);
  3643. struct llist_node *freed;
  3644. /* All file-owned VMA should have been released by this point through
  3645. * i915_gem_close_object(), or earlier by i915_gem_context_close().
  3646. * However, the object may also be bound into the global GTT (e.g.
  3647. * older GPUs without per-process support, or for direct access through
  3648. * the GTT either for the user or for scanout). Those VMA still need to
  3649. * unbound now.
  3650. */
  3651. while ((freed = llist_del_all(&i915->mm.free_list)))
  3652. __i915_gem_free_objects(i915, freed);
  3653. }
  3654. static void __i915_gem_free_object_rcu(struct rcu_head *head)
  3655. {
  3656. struct drm_i915_gem_object *obj =
  3657. container_of(head, typeof(*obj), rcu);
  3658. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3659. /* We can't simply use call_rcu() from i915_gem_free_object()
  3660. * as we need to block whilst unbinding, and the call_rcu
  3661. * task may be called from softirq context. So we take a
  3662. * detour through a worker.
  3663. */
  3664. if (llist_add(&obj->freed, &i915->mm.free_list))
  3665. schedule_work(&i915->mm.free_work);
  3666. }
  3667. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3668. {
  3669. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3670. if (obj->mm.quirked)
  3671. __i915_gem_object_unpin_pages(obj);
  3672. if (discard_backing_storage(obj))
  3673. obj->mm.madv = I915_MADV_DONTNEED;
  3674. /* Before we free the object, make sure any pure RCU-only
  3675. * read-side critical sections are complete, e.g.
  3676. * i915_gem_busy_ioctl(). For the corresponding synchronized
  3677. * lookup see i915_gem_object_lookup_rcu().
  3678. */
  3679. call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
  3680. }
  3681. void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
  3682. {
  3683. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3684. GEM_BUG_ON(i915_gem_object_has_active_reference(obj));
  3685. if (i915_gem_object_is_active(obj))
  3686. i915_gem_object_set_active_reference(obj);
  3687. else
  3688. i915_gem_object_put(obj);
  3689. }
  3690. static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
  3691. {
  3692. struct intel_engine_cs *engine;
  3693. enum intel_engine_id id;
  3694. for_each_engine(engine, dev_priv, id)
  3695. GEM_BUG_ON(engine->last_retired_context &&
  3696. !i915_gem_context_is_kernel(engine->last_retired_context));
  3697. }
  3698. void i915_gem_sanitize(struct drm_i915_private *i915)
  3699. {
  3700. /*
  3701. * If we inherit context state from the BIOS or earlier occupants
  3702. * of the GPU, the GPU may be in an inconsistent state when we
  3703. * try to take over. The only way to remove the earlier state
  3704. * is by resetting. However, resetting on earlier gen is tricky as
  3705. * it may impact the display and we are uncertain about the stability
  3706. * of the reset, so we only reset recent machines with logical
  3707. * context support (that must be reset to remove any stray contexts).
  3708. */
  3709. if (HAS_HW_CONTEXTS(i915)) {
  3710. int reset = intel_gpu_reset(i915, ALL_ENGINES);
  3711. WARN_ON(reset && reset != -ENODEV);
  3712. }
  3713. }
  3714. int i915_gem_suspend(struct drm_i915_private *dev_priv)
  3715. {
  3716. struct drm_device *dev = &dev_priv->drm;
  3717. int ret;
  3718. intel_runtime_pm_get(dev_priv);
  3719. intel_suspend_gt_powersave(dev_priv);
  3720. mutex_lock(&dev->struct_mutex);
  3721. /* We have to flush all the executing contexts to main memory so
  3722. * that they can saved in the hibernation image. To ensure the last
  3723. * context image is coherent, we have to switch away from it. That
  3724. * leaves the dev_priv->kernel_context still active when
  3725. * we actually suspend, and its image in memory may not match the GPU
  3726. * state. Fortunately, the kernel_context is disposable and we do
  3727. * not rely on its state.
  3728. */
  3729. ret = i915_gem_switch_to_kernel_context(dev_priv);
  3730. if (ret)
  3731. goto err_unlock;
  3732. ret = i915_gem_wait_for_idle(dev_priv,
  3733. I915_WAIT_INTERRUPTIBLE |
  3734. I915_WAIT_LOCKED);
  3735. if (ret)
  3736. goto err_unlock;
  3737. assert_kernel_context_is_current(dev_priv);
  3738. i915_gem_context_lost(dev_priv);
  3739. mutex_unlock(&dev->struct_mutex);
  3740. intel_guc_suspend(dev_priv);
  3741. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  3742. cancel_delayed_work_sync(&dev_priv->gt.retire_work);
  3743. /* As the idle_work is rearming if it detects a race, play safe and
  3744. * repeat the flush until it is definitely idle.
  3745. */
  3746. while (flush_delayed_work(&dev_priv->gt.idle_work))
  3747. ;
  3748. i915_gem_drain_freed_objects(dev_priv);
  3749. /* Assert that we sucessfully flushed all the work and
  3750. * reset the GPU back to its idle, low power state.
  3751. */
  3752. WARN_ON(dev_priv->gt.awake);
  3753. WARN_ON(!intel_engines_are_idle(dev_priv));
  3754. /*
  3755. * Neither the BIOS, ourselves or any other kernel
  3756. * expects the system to be in execlists mode on startup,
  3757. * so we need to reset the GPU back to legacy mode. And the only
  3758. * known way to disable logical contexts is through a GPU reset.
  3759. *
  3760. * So in order to leave the system in a known default configuration,
  3761. * always reset the GPU upon unload and suspend. Afterwards we then
  3762. * clean up the GEM state tracking, flushing off the requests and
  3763. * leaving the system in a known idle state.
  3764. *
  3765. * Note that is of the upmost importance that the GPU is idle and
  3766. * all stray writes are flushed *before* we dismantle the backing
  3767. * storage for the pinned objects.
  3768. *
  3769. * However, since we are uncertain that resetting the GPU on older
  3770. * machines is a good idea, we don't - just in case it leaves the
  3771. * machine in an unusable condition.
  3772. */
  3773. i915_gem_sanitize(dev_priv);
  3774. goto out_rpm_put;
  3775. err_unlock:
  3776. mutex_unlock(&dev->struct_mutex);
  3777. out_rpm_put:
  3778. intel_runtime_pm_put(dev_priv);
  3779. return ret;
  3780. }
  3781. void i915_gem_resume(struct drm_i915_private *dev_priv)
  3782. {
  3783. struct drm_device *dev = &dev_priv->drm;
  3784. WARN_ON(dev_priv->gt.awake);
  3785. mutex_lock(&dev->struct_mutex);
  3786. i915_gem_restore_gtt_mappings(dev_priv);
  3787. /* As we didn't flush the kernel context before suspend, we cannot
  3788. * guarantee that the context image is complete. So let's just reset
  3789. * it and start again.
  3790. */
  3791. dev_priv->gt.resume(dev_priv);
  3792. mutex_unlock(&dev->struct_mutex);
  3793. }
  3794. void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
  3795. {
  3796. if (INTEL_GEN(dev_priv) < 5 ||
  3797. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3798. return;
  3799. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3800. DISP_TILE_SURFACE_SWIZZLING);
  3801. if (IS_GEN5(dev_priv))
  3802. return;
  3803. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3804. if (IS_GEN6(dev_priv))
  3805. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3806. else if (IS_GEN7(dev_priv))
  3807. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3808. else if (IS_GEN8(dev_priv))
  3809. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3810. else
  3811. BUG();
  3812. }
  3813. static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
  3814. {
  3815. I915_WRITE(RING_CTL(base), 0);
  3816. I915_WRITE(RING_HEAD(base), 0);
  3817. I915_WRITE(RING_TAIL(base), 0);
  3818. I915_WRITE(RING_START(base), 0);
  3819. }
  3820. static void init_unused_rings(struct drm_i915_private *dev_priv)
  3821. {
  3822. if (IS_I830(dev_priv)) {
  3823. init_unused_ring(dev_priv, PRB1_BASE);
  3824. init_unused_ring(dev_priv, SRB0_BASE);
  3825. init_unused_ring(dev_priv, SRB1_BASE);
  3826. init_unused_ring(dev_priv, SRB2_BASE);
  3827. init_unused_ring(dev_priv, SRB3_BASE);
  3828. } else if (IS_GEN2(dev_priv)) {
  3829. init_unused_ring(dev_priv, SRB0_BASE);
  3830. init_unused_ring(dev_priv, SRB1_BASE);
  3831. } else if (IS_GEN3(dev_priv)) {
  3832. init_unused_ring(dev_priv, PRB1_BASE);
  3833. init_unused_ring(dev_priv, PRB2_BASE);
  3834. }
  3835. }
  3836. static int __i915_gem_restart_engines(void *data)
  3837. {
  3838. struct drm_i915_private *i915 = data;
  3839. struct intel_engine_cs *engine;
  3840. enum intel_engine_id id;
  3841. int err;
  3842. for_each_engine(engine, i915, id) {
  3843. err = engine->init_hw(engine);
  3844. if (err)
  3845. return err;
  3846. }
  3847. return 0;
  3848. }
  3849. int i915_gem_init_hw(struct drm_i915_private *dev_priv)
  3850. {
  3851. int ret;
  3852. dev_priv->gt.last_init_time = ktime_get();
  3853. /* Double layer security blanket, see i915_gem_init() */
  3854. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3855. if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
  3856. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3857. if (IS_HASWELL(dev_priv))
  3858. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
  3859. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3860. if (HAS_PCH_NOP(dev_priv)) {
  3861. if (IS_IVYBRIDGE(dev_priv)) {
  3862. u32 temp = I915_READ(GEN7_MSG_CTL);
  3863. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3864. I915_WRITE(GEN7_MSG_CTL, temp);
  3865. } else if (INTEL_GEN(dev_priv) >= 7) {
  3866. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3867. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3868. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3869. }
  3870. }
  3871. i915_gem_init_swizzling(dev_priv);
  3872. /*
  3873. * At least 830 can leave some of the unused rings
  3874. * "active" (ie. head != tail) after resume which
  3875. * will prevent c3 entry. Makes sure all unused rings
  3876. * are totally idle.
  3877. */
  3878. init_unused_rings(dev_priv);
  3879. BUG_ON(!dev_priv->kernel_context);
  3880. ret = i915_ppgtt_init_hw(dev_priv);
  3881. if (ret) {
  3882. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  3883. goto out;
  3884. }
  3885. /* Need to do basic initialisation of all rings first: */
  3886. ret = __i915_gem_restart_engines(dev_priv);
  3887. if (ret)
  3888. goto out;
  3889. intel_mocs_init_l3cc_table(dev_priv);
  3890. /* We can't enable contexts until all firmware is loaded */
  3891. ret = intel_uc_init_hw(dev_priv);
  3892. if (ret)
  3893. goto out;
  3894. out:
  3895. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3896. return ret;
  3897. }
  3898. bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
  3899. {
  3900. if (INTEL_INFO(dev_priv)->gen < 6)
  3901. return false;
  3902. /* TODO: make semaphores and Execlists play nicely together */
  3903. if (i915.enable_execlists)
  3904. return false;
  3905. if (value >= 0)
  3906. return value;
  3907. #ifdef CONFIG_INTEL_IOMMU
  3908. /* Enable semaphores on SNB when IO remapping is off */
  3909. if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
  3910. return false;
  3911. #endif
  3912. return true;
  3913. }
  3914. int i915_gem_init(struct drm_i915_private *dev_priv)
  3915. {
  3916. int ret;
  3917. mutex_lock(&dev_priv->drm.struct_mutex);
  3918. i915_gem_clflush_init(dev_priv);
  3919. if (!i915.enable_execlists) {
  3920. dev_priv->gt.resume = intel_legacy_submission_resume;
  3921. dev_priv->gt.cleanup_engine = intel_engine_cleanup;
  3922. } else {
  3923. dev_priv->gt.resume = intel_lr_context_resume;
  3924. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  3925. }
  3926. /* This is just a security blanket to placate dragons.
  3927. * On some systems, we very sporadically observe that the first TLBs
  3928. * used by the CS may be stale, despite us poking the TLB reset. If
  3929. * we hold the forcewake during initialisation these problems
  3930. * just magically go away.
  3931. */
  3932. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3933. i915_gem_init_userptr(dev_priv);
  3934. ret = i915_gem_init_ggtt(dev_priv);
  3935. if (ret)
  3936. goto out_unlock;
  3937. ret = i915_gem_context_init(dev_priv);
  3938. if (ret)
  3939. goto out_unlock;
  3940. ret = intel_engines_init(dev_priv);
  3941. if (ret)
  3942. goto out_unlock;
  3943. ret = i915_gem_init_hw(dev_priv);
  3944. if (ret == -EIO) {
  3945. /* Allow engine initialisation to fail by marking the GPU as
  3946. * wedged. But we only want to do this where the GPU is angry,
  3947. * for all other failure, such as an allocation failure, bail.
  3948. */
  3949. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  3950. i915_gem_set_wedged(dev_priv);
  3951. ret = 0;
  3952. }
  3953. out_unlock:
  3954. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3955. mutex_unlock(&dev_priv->drm.struct_mutex);
  3956. return ret;
  3957. }
  3958. void i915_gem_init_mmio(struct drm_i915_private *i915)
  3959. {
  3960. i915_gem_sanitize(i915);
  3961. }
  3962. void
  3963. i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
  3964. {
  3965. struct intel_engine_cs *engine;
  3966. enum intel_engine_id id;
  3967. for_each_engine(engine, dev_priv, id)
  3968. dev_priv->gt.cleanup_engine(engine);
  3969. }
  3970. void
  3971. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  3972. {
  3973. int i;
  3974. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  3975. !IS_CHERRYVIEW(dev_priv))
  3976. dev_priv->num_fence_regs = 32;
  3977. else if (INTEL_INFO(dev_priv)->gen >= 4 ||
  3978. IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  3979. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
  3980. dev_priv->num_fence_regs = 16;
  3981. else
  3982. dev_priv->num_fence_regs = 8;
  3983. if (intel_vgpu_active(dev_priv))
  3984. dev_priv->num_fence_regs =
  3985. I915_READ(vgtif_reg(avail_rs.fence_num));
  3986. /* Initialize fence registers to zero */
  3987. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  3988. struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
  3989. fence->i915 = dev_priv;
  3990. fence->id = i;
  3991. list_add_tail(&fence->link, &dev_priv->mm.fence_list);
  3992. }
  3993. i915_gem_restore_fences(dev_priv);
  3994. i915_gem_detect_bit_6_swizzle(dev_priv);
  3995. }
  3996. int
  3997. i915_gem_load_init(struct drm_i915_private *dev_priv)
  3998. {
  3999. int err = -ENOMEM;
  4000. dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
  4001. if (!dev_priv->objects)
  4002. goto err_out;
  4003. dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
  4004. if (!dev_priv->vmas)
  4005. goto err_objects;
  4006. dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
  4007. SLAB_HWCACHE_ALIGN |
  4008. SLAB_RECLAIM_ACCOUNT |
  4009. SLAB_TYPESAFE_BY_RCU);
  4010. if (!dev_priv->requests)
  4011. goto err_vmas;
  4012. dev_priv->dependencies = KMEM_CACHE(i915_dependency,
  4013. SLAB_HWCACHE_ALIGN |
  4014. SLAB_RECLAIM_ACCOUNT);
  4015. if (!dev_priv->dependencies)
  4016. goto err_requests;
  4017. mutex_lock(&dev_priv->drm.struct_mutex);
  4018. INIT_LIST_HEAD(&dev_priv->gt.timelines);
  4019. err = i915_gem_timeline_init__global(dev_priv);
  4020. mutex_unlock(&dev_priv->drm.struct_mutex);
  4021. if (err)
  4022. goto err_dependencies;
  4023. INIT_LIST_HEAD(&dev_priv->context_list);
  4024. INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
  4025. init_llist_head(&dev_priv->mm.free_list);
  4026. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4027. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4028. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4029. INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
  4030. INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
  4031. i915_gem_retire_work_handler);
  4032. INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
  4033. i915_gem_idle_work_handler);
  4034. init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
  4035. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4036. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4037. dev_priv->mm.interruptible = true;
  4038. atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
  4039. spin_lock_init(&dev_priv->fb_tracking.lock);
  4040. return 0;
  4041. err_dependencies:
  4042. kmem_cache_destroy(dev_priv->dependencies);
  4043. err_requests:
  4044. kmem_cache_destroy(dev_priv->requests);
  4045. err_vmas:
  4046. kmem_cache_destroy(dev_priv->vmas);
  4047. err_objects:
  4048. kmem_cache_destroy(dev_priv->objects);
  4049. err_out:
  4050. return err;
  4051. }
  4052. void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
  4053. {
  4054. i915_gem_drain_freed_objects(dev_priv);
  4055. WARN_ON(!llist_empty(&dev_priv->mm.free_list));
  4056. WARN_ON(dev_priv->mm.object_count);
  4057. mutex_lock(&dev_priv->drm.struct_mutex);
  4058. i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
  4059. WARN_ON(!list_empty(&dev_priv->gt.timelines));
  4060. mutex_unlock(&dev_priv->drm.struct_mutex);
  4061. kmem_cache_destroy(dev_priv->dependencies);
  4062. kmem_cache_destroy(dev_priv->requests);
  4063. kmem_cache_destroy(dev_priv->vmas);
  4064. kmem_cache_destroy(dev_priv->objects);
  4065. /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
  4066. rcu_barrier();
  4067. }
  4068. int i915_gem_freeze(struct drm_i915_private *dev_priv)
  4069. {
  4070. mutex_lock(&dev_priv->drm.struct_mutex);
  4071. i915_gem_shrink_all(dev_priv);
  4072. mutex_unlock(&dev_priv->drm.struct_mutex);
  4073. return 0;
  4074. }
  4075. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  4076. {
  4077. struct drm_i915_gem_object *obj;
  4078. struct list_head *phases[] = {
  4079. &dev_priv->mm.unbound_list,
  4080. &dev_priv->mm.bound_list,
  4081. NULL
  4082. }, **p;
  4083. /* Called just before we write the hibernation image.
  4084. *
  4085. * We need to update the domain tracking to reflect that the CPU
  4086. * will be accessing all the pages to create and restore from the
  4087. * hibernation, and so upon restoration those pages will be in the
  4088. * CPU domain.
  4089. *
  4090. * To make sure the hibernation image contains the latest state,
  4091. * we update that state just before writing out the image.
  4092. *
  4093. * To try and reduce the hibernation image, we manually shrink
  4094. * the objects as well.
  4095. */
  4096. mutex_lock(&dev_priv->drm.struct_mutex);
  4097. i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
  4098. for (p = phases; *p; p++) {
  4099. list_for_each_entry(obj, *p, global_link) {
  4100. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4101. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4102. }
  4103. }
  4104. mutex_unlock(&dev_priv->drm.struct_mutex);
  4105. return 0;
  4106. }
  4107. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4108. {
  4109. struct drm_i915_file_private *file_priv = file->driver_priv;
  4110. struct drm_i915_gem_request *request;
  4111. /* Clean up our request list when the client is going away, so that
  4112. * later retire_requests won't dereference our soon-to-be-gone
  4113. * file_priv.
  4114. */
  4115. spin_lock(&file_priv->mm.lock);
  4116. list_for_each_entry(request, &file_priv->mm.request_list, client_link)
  4117. request->file_priv = NULL;
  4118. spin_unlock(&file_priv->mm.lock);
  4119. if (!list_empty(&file_priv->rps.link)) {
  4120. spin_lock(&to_i915(dev)->rps.client_lock);
  4121. list_del(&file_priv->rps.link);
  4122. spin_unlock(&to_i915(dev)->rps.client_lock);
  4123. }
  4124. }
  4125. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4126. {
  4127. struct drm_i915_file_private *file_priv;
  4128. int ret;
  4129. DRM_DEBUG("\n");
  4130. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4131. if (!file_priv)
  4132. return -ENOMEM;
  4133. file->driver_priv = file_priv;
  4134. file_priv->dev_priv = to_i915(dev);
  4135. file_priv->file = file;
  4136. INIT_LIST_HEAD(&file_priv->rps.link);
  4137. spin_lock_init(&file_priv->mm.lock);
  4138. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4139. file_priv->bsd_engine = -1;
  4140. ret = i915_gem_context_open(dev, file);
  4141. if (ret)
  4142. kfree(file_priv);
  4143. return ret;
  4144. }
  4145. /**
  4146. * i915_gem_track_fb - update frontbuffer tracking
  4147. * @old: current GEM buffer for the frontbuffer slots
  4148. * @new: new GEM buffer for the frontbuffer slots
  4149. * @frontbuffer_bits: bitmask of frontbuffer slots
  4150. *
  4151. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4152. * from @old and setting them in @new. Both @old and @new can be NULL.
  4153. */
  4154. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4155. struct drm_i915_gem_object *new,
  4156. unsigned frontbuffer_bits)
  4157. {
  4158. /* Control of individual bits within the mask are guarded by
  4159. * the owning plane->mutex, i.e. we can never see concurrent
  4160. * manipulation of individual bits. But since the bitfield as a whole
  4161. * is updated using RMW, we need to use atomics in order to update
  4162. * the bits.
  4163. */
  4164. BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
  4165. sizeof(atomic_t) * BITS_PER_BYTE);
  4166. if (old) {
  4167. WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
  4168. atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
  4169. }
  4170. if (new) {
  4171. WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
  4172. atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
  4173. }
  4174. }
  4175. /* Allocate a new GEM object and fill it with the supplied data */
  4176. struct drm_i915_gem_object *
  4177. i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
  4178. const void *data, size_t size)
  4179. {
  4180. struct drm_i915_gem_object *obj;
  4181. struct file *file;
  4182. size_t offset;
  4183. int err;
  4184. obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
  4185. if (IS_ERR(obj))
  4186. return obj;
  4187. GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
  4188. file = obj->base.filp;
  4189. offset = 0;
  4190. do {
  4191. unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
  4192. struct page *page;
  4193. void *pgdata, *vaddr;
  4194. err = pagecache_write_begin(file, file->f_mapping,
  4195. offset, len, 0,
  4196. &page, &pgdata);
  4197. if (err < 0)
  4198. goto fail;
  4199. vaddr = kmap(page);
  4200. memcpy(vaddr, data, len);
  4201. kunmap(page);
  4202. err = pagecache_write_end(file, file->f_mapping,
  4203. offset, len, len,
  4204. page, pgdata);
  4205. if (err < 0)
  4206. goto fail;
  4207. size -= len;
  4208. data += len;
  4209. offset += len;
  4210. } while (size);
  4211. return obj;
  4212. fail:
  4213. i915_gem_object_put(obj);
  4214. return ERR_PTR(err);
  4215. }
  4216. struct scatterlist *
  4217. i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
  4218. unsigned int n,
  4219. unsigned int *offset)
  4220. {
  4221. struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
  4222. struct scatterlist *sg;
  4223. unsigned int idx, count;
  4224. might_sleep();
  4225. GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
  4226. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
  4227. /* As we iterate forward through the sg, we record each entry in a
  4228. * radixtree for quick repeated (backwards) lookups. If we have seen
  4229. * this index previously, we will have an entry for it.
  4230. *
  4231. * Initial lookup is O(N), but this is amortized to O(1) for
  4232. * sequential page access (where each new request is consecutive
  4233. * to the previous one). Repeated lookups are O(lg(obj->base.size)),
  4234. * i.e. O(1) with a large constant!
  4235. */
  4236. if (n < READ_ONCE(iter->sg_idx))
  4237. goto lookup;
  4238. mutex_lock(&iter->lock);
  4239. /* We prefer to reuse the last sg so that repeated lookup of this
  4240. * (or the subsequent) sg are fast - comparing against the last
  4241. * sg is faster than going through the radixtree.
  4242. */
  4243. sg = iter->sg_pos;
  4244. idx = iter->sg_idx;
  4245. count = __sg_page_count(sg);
  4246. while (idx + count <= n) {
  4247. unsigned long exception, i;
  4248. int ret;
  4249. /* If we cannot allocate and insert this entry, or the
  4250. * individual pages from this range, cancel updating the
  4251. * sg_idx so that on this lookup we are forced to linearly
  4252. * scan onwards, but on future lookups we will try the
  4253. * insertion again (in which case we need to be careful of
  4254. * the error return reporting that we have already inserted
  4255. * this index).
  4256. */
  4257. ret = radix_tree_insert(&iter->radix, idx, sg);
  4258. if (ret && ret != -EEXIST)
  4259. goto scan;
  4260. exception =
  4261. RADIX_TREE_EXCEPTIONAL_ENTRY |
  4262. idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
  4263. for (i = 1; i < count; i++) {
  4264. ret = radix_tree_insert(&iter->radix, idx + i,
  4265. (void *)exception);
  4266. if (ret && ret != -EEXIST)
  4267. goto scan;
  4268. }
  4269. idx += count;
  4270. sg = ____sg_next(sg);
  4271. count = __sg_page_count(sg);
  4272. }
  4273. scan:
  4274. iter->sg_pos = sg;
  4275. iter->sg_idx = idx;
  4276. mutex_unlock(&iter->lock);
  4277. if (unlikely(n < idx)) /* insertion completed by another thread */
  4278. goto lookup;
  4279. /* In case we failed to insert the entry into the radixtree, we need
  4280. * to look beyond the current sg.
  4281. */
  4282. while (idx + count <= n) {
  4283. idx += count;
  4284. sg = ____sg_next(sg);
  4285. count = __sg_page_count(sg);
  4286. }
  4287. *offset = n - idx;
  4288. return sg;
  4289. lookup:
  4290. rcu_read_lock();
  4291. sg = radix_tree_lookup(&iter->radix, n);
  4292. GEM_BUG_ON(!sg);
  4293. /* If this index is in the middle of multi-page sg entry,
  4294. * the radixtree will contain an exceptional entry that points
  4295. * to the start of that range. We will return the pointer to
  4296. * the base page and the offset of this page within the
  4297. * sg entry's range.
  4298. */
  4299. *offset = 0;
  4300. if (unlikely(radix_tree_exception(sg))) {
  4301. unsigned long base =
  4302. (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
  4303. sg = radix_tree_lookup(&iter->radix, base);
  4304. GEM_BUG_ON(!sg);
  4305. *offset = n - base;
  4306. }
  4307. rcu_read_unlock();
  4308. return sg;
  4309. }
  4310. struct page *
  4311. i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
  4312. {
  4313. struct scatterlist *sg;
  4314. unsigned int offset;
  4315. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  4316. sg = i915_gem_object_get_sg(obj, n, &offset);
  4317. return nth_page(sg_page(sg), offset);
  4318. }
  4319. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  4320. struct page *
  4321. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
  4322. unsigned int n)
  4323. {
  4324. struct page *page;
  4325. page = i915_gem_object_get_page(obj, n);
  4326. if (!obj->mm.dirty)
  4327. set_page_dirty(page);
  4328. return page;
  4329. }
  4330. dma_addr_t
  4331. i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
  4332. unsigned long n)
  4333. {
  4334. struct scatterlist *sg;
  4335. unsigned int offset;
  4336. sg = i915_gem_object_get_sg(obj, n, &offset);
  4337. return sg_dma_address(sg) + (offset << PAGE_SHIFT);
  4338. }
  4339. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  4340. #include "selftests/scatterlist.c"
  4341. #include "selftests/mock_gem_device.c"
  4342. #include "selftests/huge_gem_object.c"
  4343. #include "selftests/i915_gem_object.c"
  4344. #include "selftests/i915_gem_coherency.c"
  4345. #endif